PHP Classes & Objects
Defining Classes
Classes are defined like so:
class identifier {
property_definitions
function_definitions
}
Function definition may include the definition of a constructor.
An object of a class is created using:
new identifier(arg1, arg2, ...)
The definition of a class typically looks like so:
class identifier {
# Properties
vis $attrib1
...
vis $attribN = value
# Constructor
function __construct(p1, ...) i{
statements
}
# Methods
vis function method1(p1, ...) {
statements
}
vis function methodN(p1, ...) {
statements
}
}
vis
is a declaration of visbility.
We can access the methods and objects of a class like so:
obj->attrib1 ...
obj->method1(a1) ...
$this
We can use the pseudo-variable $this
as a reference to the current object like so:
class Rectangle {
protected $height;
protected $width;
function __construct($height, $width) {
$this->width = $width;
$this->height = $height;
}
}
Visibility
Properties and methods can have the following visibilities:
public
- Accessible everywhere.private
- Accessible only within the same class.protected
- Accessible only within the class itself, by inheriting and parent classes.
Properties require a visibility declaration whereas, by default, methods are public
.
Constants
Classes can have their own constants. By default they are declared public
:
- Class constants are allocated once per class and not for each class instance.
- Class constants are accessed using the scope resolution operator
::
:
class MyClass {
const SIZE = 10;
}
echo MyClass::SIZE; # prints 10
$o = new MyClass();
echo $o::SIZE; # prints 10
Destructors
A class can have a destructor method that is called as soon as there are no references to a particular object:
class Employee {
static $totalNumber = 0;
public $name;
function __construct($name) {
$this->name = $name;
Employee::$totalNumber++
}
function __destruct() {
Employee::$totalNumber--;
}
}
$e1 = new Employee("Ada");
$e2 = new Employee("Ben");
echo Employee:$totalNumber # prints 2
$e1 = null;
echo Employee::$totalNumber # prints 1
Inheritance
Inheritance can be implemented like so:
class Rectangle {
protected $height;
protected $width;
function __construct($height, $width) {
$this->height = $height;
$this->width = $width;
}
function area() {
return $this->width * $this->height;
}
}
class Square extends Rectangle {
function __construct($size) {
parent::__construct($size, $size);
}
}
$rt = new Rectangle(3, 4);
echo "\$rt1 area = ", $rt1->area(), "\n";
$sq1 = new Square(5);
echo "\$sq1 area = ", $sq1->area(), "\n";
$rt1 area = 12
$sq1 area = 25
- The constructor of the parent class is not automatically called. It must be called explicitly from the child class.
- Inherited constants, properties and methods can be overridden by re-declaring them with the same name defined in the parent class.
- The declaration
final
can be used to prevent a method from being overridden. - Using
parent::
it is possible to access overridden methods or static properties of the parent class. - Using
self::
it is possible to access static properties and methods of the current class.
Interfaces
Interfaces specify which methods a class must implement, without providing an implementation:
interface Shape {
public function area();
}
class Rectangle implements Shape {
...
}
- All methods in an interface must be declared public.
Introspection Functions
Function | Description |
---|---|
class_exists(class) |
Returns TRUE if a class class exists. |
get_class(obj) |
Returns the name of the class to which an object belongs. |
is_a(obj, class) |
Returns TRUE if obj is an instance of the class named class . |
method_exists(obj, method) |
Returns TRUE if obj has a method named method . |
property_exists(obj, property) |
Returns TRUE if obj has an property named property . |
get_object_vars(object) |
Returns an array with the accessible non-static properties of object mapped to their values. |
get_class_methods(class) |
Returns an array of method names for class . |
property
and class
are read in as strings.