Object Model
The new OOP features in PHP5 is probably the one thing that everyone knows for sure about. Out of all the new features, these are the ones that are talked about most!
Passed by Reference
This is an important change. In PHP4, everything was passed by value, including objects. This has changed in PHP5 — all objects are now passed by reference.
PHP Code:
$joe = new Person();
$joe->sex = ‘male’;$betty = $joe;
$betty->sex = ‘female’;echo $joe->sex; // Will be ‘female’
The above code fragment was common in PHP4. If you needed to duplicate an object, you simply copied it by assigning it to another variable. But in PHP5 you must use the new clone keyword.Note that this also means you can stop using the reference operator (&). It was common practice to pass your objects around using the & operator to get around the annoying pass-by-value functionality in PHP4.
Class Constants and Static Methods/Properties
You can now create class constants that act much the same was as define()’ed constants, but are contained within a class definition and accessed with the :: operator.
Static methods and properties are also available. When you declare a class member as static, then it makes that member accessible (through the :: operator) without an instance. (Note this means within methods, the $this variable is not available)
Visibility
Class methods and properties now have visibility. PHP has 3 levels of visibility: - Public is the most visible, making methods accessible to everyone and properties readable and writable by everyone.
- Protected makes members accessible to the class itself and any subclasses as well as any parent classes.
- Private makes members only available to the class itself.
< removed link drops>
Last edited by chrishirst; 08-30-2008 at 08:11 AM..
|