"; } // Member variables var $bar; } $f = new foo(); $fp = new foo( "Tom" ); $f->foobar();
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'.
Private, Protected, etc is not supported by PHP, everything is public.
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.
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_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
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.