Objects
Basic object-oriented programming functionality was added in PHP 3. Object handling was completely rewritten for PHP 5, expanding the feature set and enhancing performance. In previous versions of PHP, objects were handled like primitive types. The drawback of this method was that the whole object was copied when a variable was assigned or passed as a parameter to a method. In the new approach, objects are referenced by handle, and not by value. PHP 5 introduced private and protected member variables and methods, along with abstract classes and final classes as well as abstract methods and final methods. It also introduced a standard way of declaring constructors and destructors, similar to that of other object-oriented languages such as C++, and a standard exception handling model. Furthermore PHP 5 added Interfaces and allows for multiple Interfaces to be implemented. There are special interfaces that allow objects to interact with the runtime system. Objects implementing ArrayAccess can be used with array syntax and objects implementing Iterator or IteratorAggregate can be used with the foreach language construct. The static method and class variable features in Zend Engine 2 do not work the way some would expect. There is no virtual table feature in the engine, so static variables are bound with a name instead of a reference at compile time.
This example shows how to define a class, foo, that inherits from class bar. The function mystaticfunc is a public static function that can be called with foo::mystaticfunc;.
class foo extends bar { function __construct { $doo = "wah dee dee"; } public static function mystaticfunc { $dee = "dee dee dum"; } }If the developer creates a copy of an object using the reserved word clone, the Zend engine will check if a __clone method has been defined or not. If not, it will call a default __clone which will copy the object's properties. If a __clone method is defined, then it will be responsible for setting the necessary properties in the created object. For convenience, the engine will supply a function that imports the properties of the source object, so that the programmer can start with a by-value replica of the source object and only override properties that need to be changed.
Read more about this topic: PHP Syntax And Semantics
Famous quotes containing the word objects:
“It is an inexpressible Pleasure to know a little of the World, and be of no Character or Significancy in it. To be ever unconcerned, and ever looking on new Objects with an endless Curiosity, is a Delight known only to those who are turned for Speculation: Nay, they who enjoy it, must value things only as they are the Objects of Speculation, without drawing any worldly Advantage to themselves from them, but just as they are what contribute to their Amusement, or the Improvement of the Mind.”
—Richard Steele (16721729)
“Luckless is the country in which the symbols of procreation are the objects of shame, while the agents of destruction are honored! And yet you call that member your pudendum, or shameful part, as if there were anything more glorious than creating life, or anything more atrocious than taking it away.”
—Savinien Cyrano De Bergerac (16191655)
“But what is classification but the perceiving that these objects are not chaotic, and are not foreign, but have a law which is also the law of the human mind?”
—Ralph Waldo Emerson (18031882)