Contents / Previous / Next


Objects

class: A class is a collection of variables and functions, defined using the following syntax: class foo { // Constructors function foo( $p = "Joe" ) { $this->bar = $p; } // Member functions function foobar() { echo "foobar $this->bar <p>"; } // Member variables var $bar; } $f = new foo(); $fp = new foo( "Tom" ); $f->foobar(); Constructors: Constructors are functions in a class that are automatically called when you create a new instance of a class with new. A function becomes a constructor, when it has the same name as the class it is defined in.

Destructors: Destructors are functions that are called automatically when an object is destroyed, either with unset() or by simply going out of scope. There are no destructors in PHP. You may use register_shutdown_function() instead to simulate most effects of destructors.

Instance = Variable of a class type: You create a variable of a class type with the new operator.

Member Variables and Functions: You access class members with the instance-name followed by -> and the name of the member, e.g., "$f->foobar();".

this: To access it's own functions and variables from within a class, one can use the pseudo-variable $this which can be read as 'my own' or 'current object'.


Inheritance

Classes can be extensions of other classes. The extended or derived class has all variables and functions of the base class (this is called 'inheritance').
An extended class is always dependent on a single base class, that is, multiple inheritance is not supported. Classes are extended using the keyword extends: class A { } class B extends A { } This is also called a "parent-child" relationship. You create a class, parent, and use extends to create a new class based on the parent class: the child class. You can use this new child class and create another class based on this child class.

Private, Protected, etc is not supported by PHP, everything is public.


The :: Operator

Sometimes it is useful to refer to functions and variables in base classes or to refer to functions in classes that have not yet any instances. The :: operator is being used for this: class A { function example() { echo "original function A::example().<br>"; } } class B extends A { function example() { echo "redefined function B::example().<br>"; A::example(); } } // there is no object of class A. A::example(); // create an object of class B. $b = new B; $b->example(); // Output: // redefined function B::example().<br> // original function A::example().<br> The above example calls the function example() in class A, but there is no object of class A, so that we cannot write $a->example() or similar. Instead we call example() as a 'class function', that is, as a function of the class itself, not any object of that class.
There are class functions, but there are no class variables. In fact, there is no object at all at the time of the call. Thus, a class function may not use any object variables (but it can use local and global variables), and it may no use $this at all.

In the above example, class B redefines the function example(). The original definition in class A is shadowed and no longer available, unless you are refering specifically to the implementation of example() in class A using the ::-operator. Write A::example() to do this (in fact, you should be writing parent::example(), as shown below.
In this context, there is a current object and it may have object variables. Thus, when used from WITHIN an object function, you may use $this and object variables.

The mechanism of re-defining a function in a derived class is called function overriding.
There are no virtual functions in PHP. These are only useful when you work with base-class-pointers, which is possible in C++ but not in PHP.


parent

You may find yourself writing code that refers to variables and functions in base classes. This is particularly true if your derived class is a refinement or specialization of code in your base class.

Instead of using the literal name of the base class in your code, you should be using the special name parent, which refers to the name of your base class as given in the extends declaration of your class.

class A { function example() { echo "I am A::example().<br>"; } } class B extends A { function example() { echo "I am B::example().<br>"; parent::example(); } } $b = new B; $b->example(); // Output: //I am B::example(). //I am A::example().


Constructor Chaining

Constructors of base classes are not automatically called from a constructor of a derived class. It is your responsibility to propagate the call to constructors upstream where appropriate. class A { function A() { echo "Constructor of A.<br>\n"; } function example() { echo "example function in A<br>"; } } class B extends A { function B() { A::A(); parent::A(); echo "Constructor of of B.<br>\n"; parent::example(); } } // calls constructor B() which calls constructor A(): $b = new B; //Output: //Constructor of A. //Constructor of A. //Constructof of B. //example function in A


Functions Handling Classes

class_exists      : Checks if the class has been defined
get_class_methods : Returns an array of class methods' names
get_class_vars    : Returns an array of default properties
get_class         : Returns the name of the class of an object
get_declared_classes : Returns an array with the 
                       name of the defined classes
get_object_vars   : Returns an associative array of object properties
get_parent_class  : Retrieves the parent class name for object or class
is_a :              Returns TRUE if the object is of this class 
                    or has this class as one of its parents
is_subclass_of    : Returns TRUE if the object has this class 
                    as one of its parents
method_exists     : Checks if the class method exists


Serializing objects - objects in sessions

serialize() returns a string containing a byte-stream representation of any value that can be stored in PHP.
unserialize() can use this string to recreate the original variable values.

Sessions: If you are using sessions and use session_register() to register objects, these objects are serialized automatically at the end of each PHP page, and are unserialized automatically on each of the following pages. This basically means that these objects can show up on any of your pages once they become part of your session.

magic functions: PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.
The function names __sleep and __wakeup are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them.
serialize() checks if your class has a function with the magic name __sleep. If so, that function is being run prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized.
The intended use of __sleep is to close any database connections that object may have.
Conversely, unserialize() checks for the presence of a function with the magic name __wakeup. If present, this function can reconstruct any resources that object may have.
The intended use of __wakeup is to reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks.