PHP5 : Using magic methods

2013-05-15
Laurent Bientz

PHP magic methods are very useful, they can overload some native language elements.

If you're not familiar with these methods, consult the official documentation.

For our example, we will base ourselves on a class embodying a business object:

<?php
class Article extends FrontModel
{
    /**
     * Class variables
     */
    protected $_article_id;
    protected $_article_name;
    protected $_article_content;
    protected $_article_file;
    protected $_article_date_add;
    protected $_category_id;
    protected $_author_id;
    protected $_user_infos = array("mobile" => false, "desktop" => true);

    // more class methods...
}
?>

This class represents the object used to describe the overall properties of a table.

__get

Rather than having to rewrite N specific getters for all class variables, let's just magic method overloading __get :

public function __get($key)
{
    return $this->{_.$key};
}

Thus, from the instantiation of the object, you can directly access the class variables by their names as if they were public.

$article = new Article();
var_dump($article->article_name);

Public class variable article_name does not exist, PHP will automatically search overload method __get and pass it as a parameter the name of the property which is trying to access.

__set

By analogy, rather than rewriting N setters specific, let overloading the magic method __set :

public function __set($key,$value)
{
    if (!empty($value))
        $this->{_.$key} = $value;
}

Thus, from our object, we can directly assign a value to the protected class variable.

$article = new Article();
$article->article_name = "Test";
var_dump($article->article_name);

Public class variable article_name does not exist, PHP will automatically search overload method __set and pass it as a parameter the name of the property to which it is trying to access and the value that you want to assign to it.

__toString

To make life easier for developers who want to have a quick and clear vision of the object with which he works from, we will overload the method __toString.

public function __toString()
{
    eval ('$params = '.  var_export($this, true) . ';');
    var_dump($params);
}
public static function __set_state($array)
{
    return $array;
}

Thus, if we try to show our object, PHP will automatically search overloading of method __toString and call it.

$article = new Article();
$article->article_name = "title";
$article->article_name = "content";
echo $article;

Produce var_dump execution show an associative array with all the variables of the object classes and their values.

__call

To enable the developers plenty of flexibility, the method __call is convenient because it is called when attempting to access a method of unknown class. We're going to overload in order to more easily access the array keys of $_user_infos.

public static function __call($name)
{
    $key = substr(strtolower($name),2);
    return (isset($key)) ? $this->_user_infos[$key] : "";
}

Thus, the following two calls will return the values ​​of key "mobile" and "desktop" of array $_user_infos.

$article = new Article();
var_dump($article->isMobile()); # $_user_infos['mobile'] > false
var_dump($article->isDesktop()); # $_user_infos['desktop'] > true

Comment

USER EXPERIENCE

Wandi invites you to discover a new approach to navigating a website...
Are you ready?

Launch the experience