Php-mysql Course

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 HTML5 tag defines marked text? (can be used to highlight parts of text)
<mark> <embed> <span>
<p>Free corses: <mark></mark> for Web Development.</p>
Which CSS pseudo-class adds a style to an element when the mouse is over it?
:focus :hover :active
a:hover {
  font-weight: bold;
  color: #00da01;
Click on the function which returns a string value that represents the number rounded to the x digits after the decimal point.
toPrecision(x) toFixed(x) floor(x)
var num = 12.34567;
num = num.toFixed(2);
alert(num);       // 12.35
Indicate the PHP function which reads an entire file into an array.
[) file() readfile()
$arr = file("a_file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
Magic Methods __get, __set, __call, __toString

Last accessed pages

  1. SSEP - Site Search Engine PHP-Ajax (9770)
  2. querySelector and querySelectorAll (19414)
  3. PHP Method Chaining (2748)
  4. Node.js Move and Copy Directory (10181)
  5. $_GET, $_POST and $_REQUEST Variables (30794)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (459)
  2. The School for Gods (335)
  3. PHP-MySQL free course, online tutorials PHP MySQL code (227)
  4. Add, Change, and Remove Attributes with jQuery (210)
  5. PHP Unzipper - Extract Zip, Rar Archives (206)