C H A P T E R 5
■ ■ ■
Object Tools
As we have seen, PHP supports object-oriented programming through language constructs such as
classes and methods. The language also provides wider support through functions and classes designed
to help you work with objects.
In this chapter, We will look at some tools and techniques that you can use to organize, test, and
manipulate objects and classes.
This chapter will cover
- Packages: Organizing your code into logical categories
- .Namespaces: Since PHP 5.3 you can encapsulate your code elements in discrete
compartments. - Include paths: Setting central accessible locations for your library code.
- Class and object functions: Functions for testing objects, classes, properties, and
methods. - The Reflection API: A powerful suite of built-in classes that provide
unprecedented access to class information at runtime.
PHP and Packages
A package is a set of related classes, usually grouped together in some way. Packages can be used to
separate parts of a system from one another. Some programming languages formally recognize packages
and provide them with distinct namespaces. PHP has no native concept of a package, but as of PHP 5.3,
it does understand namespaces. I’ll look at this feature in the next section.
Since we will probably all have to work with older code for a while yet, I’ll go on to look at the old
way of organizing classes into package-like structures.
PHP Packages and Namespaces
Although PHP does not intrinsically support the concept of a package, developers have traditionally
used both naming schemes and the filesystem to organize their code into package-like structures. Later
on I will cover the way that you can use files and directories to organize your code. First though, I’ll look
at naming schemes, and a new, but related, feature: namespace support.
Up until PHP 5.3, developers were forced to name their files in a global context. In other words, if
you named a class ShoppingBasket, it would become instantly available across your system. This caused
two major problems. First, and most damaging, was the possibility of naming collisions. You might think