3

Someone help me understand magic methods in an easier way.

I know that magic methods are triggered at some point of a code, what I don't understand is point at which they are triggered. Like, in case of __construct(), they are triggered at point of creation of an object of the class and the parameters to be passed are optional.

PLease tell me when __get(), __set(), __isset(), __unset() are triggered in particular. It would be much helpful if stated about any other magic methods.

Hanky Panky
  • 44,997
  • 8
  • 67
  • 93
IRock
  • 127
  • 3
  • 10
  • 1
    I think you may find this thread answers some of your question: http://stackoverflow.com/questions/4713680/php-get-and-set-magic-methods – Marc Oct 19 '13 at 04:55

2 Answers2

3

PHP's magic methods all start with "__" and can only be used inside a class. I've tried to write out an example below.

class Foo
{
    private $privateVariable;
    public $publicVariable;

    public function __construct($private)
    {
        $this->privateVariable = $private;
        $this->publicVariable = "I'm public!";
    }

    // triggered when someone tries to access a private variable from the class
    public function __get($variable)
    {
        // You can do whatever you want here, you can calculate stuff etc.
        // Right now we're only accessing a private variable
        echo "Accessing the private variable " . $variable . " of the Foo class.";

        return $this->$variable;
    }

    // triggered when someone tries to change the value of a private variable
    public function __set($variable, $value)
    {
        // If you're working with a database, you have this function execute SQL queries if you like
        echo "Setting the private variable $variable of the Foo class.";

        $this->$variable = $value;
    }

    // executed when isset() is called
    public function __isset($variable)
    {
        echo "Checking if $variable is set...";

        return isset($this->$variable);
    }

    // executed when unset() is called
    public function __unset($variable)
    {
        echo "Unsetting $variable...";

        unset($this->$variable);
    }
}

$obj = new Foo("hello world");
echo $obj->privateVariable;     // hello world
echo $obj->publicVariable;      // I'm public!

$obj->privateVariable = "bar";
$obj->publicVariable = "hi world";

echo $obj->privateVariable;     // bar
echo $obj->publicVariable;      // hi world!

if (isset($obj->privateVariable))
{
    echo "Hi!";
}

unset($obj->privateVariable);

In conclusion, one of the main advantages of using these magic methods is if you want to access private variables of a class (which is against a lot of coding practices) but it does allow you to assign actions for when certain things are executed; i.e. setting variables, checking variables, etc.

As a note, __get() and __set() methods will only work for private variables.

allejo
  • 1,628
  • 3
  • 23
  • 32
0

PHP functions that start with a double underscore – a __ – are called magic functions (and/or methods) in PHP. They are functions that are always defined inside classes, and are not stand-alone (outside of classes) functions. The magic functions available in PHP are:

__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone(), and __autoload().

Now, here is an example of a class with the __construct() magic function:

class Animal {

    public $height;      // height of animal  
    public $weight;     // weight of animal

    public function __construct($height, $weight) 
    {
        $this->height = $height;  //set the height instance variable
        $this->weight = $weight; //set the weight instance variable
    }
}
Sainesh Mamgain
  • 709
  • 1
  • 10
  • 31