PHP classes

Define

class MyClass { ... }

Instantiate

Classes can be instantiated with the new keyword, which (under the hood) invokes a contructor, defined by the construct() function of the class. If the class is a child one, parent contructor must be explicitly invoked (if needed).

A destructor is invoked once the object is no longer referenced. The destructor is defined by the destruct() function of the class. Once again, if the class is a child one, parent destructor must be explicitly invoked.

Properties

Properties can be accesed with $this->property inside the class, or with $var->property outside the class.

Visibility

Properties can be private, protected or public (the default).

Constants

const constant = ...;

The right part of the assignment must be a constant expression.

Constants can be referenced with self::constant inside a class, or with class::constant outside the class.

Static properties

static property = ...;

Accessibile without instantiating an object, but can't be accessed in an instantiated object. They are referenced with self::property inside or class::property outside the class. Can be initialized with constants or literals only.

Methods

Methods can be accessed with $this->method() inside the class or with $var->method() outside the class.

Visibility

Methods can be private, protected or public (the default).

Static methods

static function( ... ) { ... }

Referenced with self::method() inside the class or with class::method() outside the class. if the final keyword is added, children class cannot overre the method.

Inheritance

class MyClass extends(parent class) { ... }

Create new subclasses which inherit anything with public or protected visibility from another class (the parent one). The final keyword prevents subclassing.

Only single inheritance is supported.

Serialization

serialize()

Creates byte-stream representation of the object.

unserialize()

Creates an object from a byte-stream representation (class must be defined first).

Abstract classes

abstract class MyClass { ... }

Abstract classes contain abstract methods and cannot be instantiated.

Abstract methods declare only their signature; must be implemented by children classes (with a matching signature) and must have the same level of visibility, or a higher one.

Interfaces

Interfaces are declared with the interface keyword, and contains onthe the methods signature.

Are implemented by classes with the implements keyword:

interface MyInterface
{
    public function myfunc_1 ( ... );
    public function myfunc_2 ( ... );
}

class MyClass implements MyInterface { ... }

More than on interface can be implemented at the same time (but method names must be unique).

Can be extended with the extends keyword; can have constants that can't be overriden by classes/interfaces that inherits them.

Overloading

Overloading provides means to dynamically interact with non existent, or non visibile, properties and methods.

Properties

Define the following public methods in the class:

  • __set()
  • __get()
  • __isset()
  • __unset()

Overloading doesn't work on static properties.

Methods

Define the following public methods in the class:

  • __call()
  • __callStatic()

Iteration

All visibile properties can be iterated through with foreach.

Custom iterations can be obtained by implementing the iterator interface.

Cloning

Objects can be cloned with che clone operator. If the class defines a _clone() method, this will be called. A swallow copy of the properties of the object will be performed.

Objects comparison

===

Check if the objects are instances of the same class, have the same attributes with the same values.

====

Check if the objects refer to the same instance.


© Alessandro Dotti Contra :: VAT # IT03617481209 :: This site uses no cookies, read our privacy policy for more information.