Object Oriented

Special Method

PHP

 

Constructor

PHP 5
class Person {
  
  public $name;
  public $age;
  
  function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
  }
  
}

Classes that have a Constructor method call this method on each newly-created object.

This method is suitable for any initialization that the object may need before it is used.

Here you can pass $name and $age when instantiating a Person object.

$person1 = new Person("Sam", 25);
 

Destructor

PHP 5
class Person {

  function __destruct() {
    print("__desctruct method called");
  }
  
}

The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.

This is a good place to close databases, files, ... connections.

 

Getter

PHP 5
class Person {
  
  public $fname;
  public $lname;
  
  public function __get($property) {
    if ($property === "fullname") {
      return $this->fname . " " . $this->lname; 
    }  
  }
  
}

You can define Getters using __get method.

If the getter method is defined, it is called whenever some name is tried to be read on an object using -> operator.

$person1 = new Person();
$person1->fname = "Sam";
$person1->lname = "Winchester";

$fullname = $person1->fullname;  // Sam Winchester

This is a good way to expose private properties.

 

Setter

PHP 5
class Person {
  
  public $fname;
  public $lname;
  
  public function __set($property, $value) {
    if ($property === "fullname") {
      $names = explode(" ", $value);
      
      $this->fname = $names[0];
      $this->lname = $names[1];
    }
  }
  
}

You can define Setters using __set method.

If the setter method is defined, it is called whenever some name is tried to be set on an object using -> operator.

$person1 = new Person();
$person1->fullname = "Sam Winchester";

$firstname = $person1->fname;  // Sam
$lastname = $person1->lname;   // Winchester

This is a good way to expose private properties.

 

toString

PHP 5
class Person {
  
  public $name;
  public $age;
  
  public function __toString() {
    return $this->name . " is " . $this->age;
  }
  
}

__toString is called whenever a string representation of the object is needed.

$person1 = new Person();
$person1->name = "Sam";
$person1->age = 25;

echo $person1;  // Sam is 25
 

Clone

PHP 5
class Person {
  
  public $name;
  public $age;
  
  public function __clone() {
    $this->name = strtoupper($this->name);
  }
  
}

The clone keyword in PHP by default creates a shallow copy of an object.

In order to have more control over cloning your class objects, you need to define __clone method.

$person1 = new Person();
$person1->name = "Sam";
$person1->age = 25;

$person2 = clone $person1;
$name = $person2->name;  // SAM
$age = $person2->age;    // 25
 

__call

PHP 5
class Person {
  
  public function __call($name, $arguments) {
    print("Method " . $name . " called.");
  }
  
}

if __call method is defined, it is triggered when any name is called as a method on an object of that class.

  • First parameter is the name of the method name.
  • Second parameter is arguments passed to the function as array.
$person1 = new Person();

$person1->doSomething();  // Method doSomething called.
 

__invoke

PHP 5
class Person {
  
  public function __invoke($x) {
    print("Person triggered with " . $x);
  }
  
}

__invoke method is called when a script tries to call an object as a function.

$person1 = new Person();
$person1(12);  // Person triggered with 12