The Basicsclass
Every class definition begins with the keyword class, followed by a class
name, which can be any name that isn't a
reserved word in PHP. Followed by a pair of curly braces, of
which contains the definition of the classes members and methods. Within
each method, except for static
methods, a pseudo variable $this is available.
$this is a reference to the same instance that
called the method.
Simple Class definition
var; /* Echo my own $var value */
}
}
?>
]]>
new
To create an instance of an object, a new object must be created and
assigned to a variable. An object will always be assigned when
creating a new object unless the object has a constructor defined that throws an exception on error.
Creating an instance
]]>
When assigning an already created instance of an object to a new variable, the new variable
will access the same instance as the object that was assigned. This
behaviour is the same when passing instances to a function. A new instance
of an already created object can be made by cloning it.
Object Assignment
var = '$assigned will have this value';
$instance = null; /* $instance and $reference become null */
var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>
]]>
&example.outputs;
string(30) "$assigned will have this value"
}
]]>
extend
A class can inherit methods and members of another class by using the
extend keyword in the declaration. It is not possible to extend multiple
classes, a class can only inherit one base class.
The inherited methods and members can be overloaded, unless the parent
class has defined a method as final, by redeclaring them
within the same name defined in the parent class. It is possible to access the overloaded
method or members by referencing them with parent::
Simple Class Inherintance
displayVar();
?>
]]>
&example.outputs;