The __get & __set Magical Functions in PHP PRO

Using these two functions we can dynamically add new variables, that weren’t defined in the class, to objects in our program.

<?
class Bongo
{
    var $vec;
    function __construct()
    {
        $this->vec = array();
    }
    function __get($str)
    {
        return $this->vec[$str];
    }
    function __set($var_name,$var_value)
    {
        $this->vec[$var_name] = $var_value;
    }
}

$ob = new Bongo();

$ob->name="balaboa";
$ob->id=12123123;

echo $ob->name." ".$ob->id;

?>

The following video clip shows how this code runs and provides more explanation.

Share:

banner for the css playlist in hebrew life michael courses for programmers

The First Steps in CSS

Learn CSS using our our videos (in Hebrew) on the CSS (he) playlist on youtube. Do it now. Do it for free.

Good Trainers Collaborate with Others

It is always essential to keep an open mind and learn from others. This applies to everyone, including teachers and especially software development trainers. Software

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Update cookies preferences