PHP provides built-in interceptor methods (also called magic methods), which are automatically executed in some special cases, when are accessed undefined methods and properties.
- Magic methods must be defined with "public" attribute.

__get() and __set() methods

The __get() and __set() methods are generally used together and are designed for working with properties that are not defined in a class (or its parents). The __get() method must be defned with one parameter and the __set() must be declared with two arguments.

  - Example:
In the following example we create a class, named AClas, with __get() and set() methods. then we create an object instance of this class.
// AClas
class AClas {
  // define a property which will store the values assigned to undefined properties
  public $prop = array();

  // __get() interceptor method
  public function __get($name) {
    // if there is an element '$name' in $prop array, outputs its value
    // else, display a message
    if(array_key_exists($name, $this->prop)) echo $this->prop[$name]. '<br />';
    else echo 'The <b>'. $name. '</b> not defined.<br />';

  // __set() interceptor method
  public function __set($name, $value) {
    // add in $prop array an element with $name as key, and $value. Then outputs a message
    $this->prop[$name] = $value;
    echo 'The <b>'. $name. '</b> not exists. It was assigned the value: <i>'. $value. "</i> to the property: <b>prop['$name']</b><br />";

// object instance of the AClas class
$obj = new AClas();

// access an undefined property
$obj->noprop;            // It will cause the execution of the __get() method

// assign a value to a undefined property
$obj->noprop = 'noprop - PHP OOP';           // will cause the execution of the __set()

// access again the same undefined property
$obj->noprop;           // the __get() is automatically invoked

// checking directly the element which __set() created it in $prop
echo 'Verificare: '. $obj->prop['noprop'];
The $prop property is defined as array to store the values of undefined properties, which are intercepted by the __set() method.
Becouse the $noprop doesn't exist in this class, accessing $obj->noprop; causes the execution of the __get() method, which gets the name of that property.
Assigning a value to "noprop" causes the execution of the __set() method, which gets the name of the property and its values and uses them to create an array element in $prop.
Se also the comments in script.
This example will output:
The noprop not defined.
The noprop not exists. It was assigned the value: noprop - PHP OOP to the property: prop['noprop']
noprop - PHP OOP
Verificare: noprop - PHP OOP

The __call() method

The __call() method is invoked when an undefined method is called. It must be defined with the following sintax:
  - Syntax:
public function __call($method, $arg_array)
$method will contain the name of the undefined method.
$arg_array is an array with the arguments passed to that undeclared method

  - Example:
In this example we create a class, named AClass, with a __call() method wihch returns a string with the name of the undefined method that is called, and all arguments passed.
// AClas class
class AClas {
  // __call() method
 public function __call($name, $args) {
    // displays a string withe the undeclared method ant its arguments
    echo 'There is no <b>'. $name. '</b> method. Arguments: <i>'. implode(', ', $args). '</i>';

// object instance of the AClas class
$obj = new AClas();

// calling an undefined method
$obj->site('coursesweb', 'tutorials');           // will automatically invoke the __call() method
This code will output:
There is no site method. Arguments: coursesweb, tutorials

toString() method

The toString() method is invoked automatically when the object instance is passed to print or echo.
  - Example:
In the following example is created a class with __construct(), a property ($msg) and a __toString() method which will return the value of $msg property.
// AClas class
class AClas {
  private $msg;       // Proprietate

  // Constructor
  public function __construct($msg) {
    // sets the value of "msg" property with the $msg attribute
    $this->msg = $msg;

  // __toString() method
  public function __toString() {
    return $this->msg;        // return the $msg property

// object instance of the AClas class
$obj = new AClas('Have a good life');

// prints the object instance as a normal variable string type
echo $obj;           // will cause the auto-calling of __toString()
echo $obj; outputs what the __toString() returns.
This example will display:
Have a good life
Without the __toString() declared in AClass, trying to print the object instance ($obj) as a string will generate an error like this:
Catchable fatal error: Object of class AClas could not be converted to string in ...

• There are other magical methods too, less used. You can find full list at the page: Magic methods.

Daily Test with Code Example

Which tag adds an image in web page?
<div> <img> <span>
<img src="" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
Magic Methods __get, __set, __call, __toString

Last accessed pages

  1. Math Lines (1555)
  2. PHP Sending E-mails (1461)
  3. Mahjongg Alchemy (4274)
  4. Drag Racer V3 (2181)
  5. Qwop (14579)

Top accessed pages

  1. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (50849)
  2. Read Excel file data in PHP - PhpExcelReader (39684)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (37891)
  4. Get Attribute (ID, Class, Name, Title, Src) with jQuery (34162)
  5. PHP PDO - exec (INSERT, UPDATE, DELETE) MySQL (31636)