__get() Method

__get() is a magic method: it is called when a property cannot be accessed, either because it doesn’t exist, or because its visibility prevents it.

That way, it is possible to create dynamically properties, without hardcoding them.

The __get() method is usually paired with the __set() method.

<?php

    class X {
        private $values = ['a' => 1,
                           'b' => 2,
                           ];

        // Checks the dictionary, or return 0
        function __get($name) {
            if (isset($this->values[$name])) {
                return $this->values[$name];
            }

            return 0;
        }
    }

?>

Documentation

See also What are magic methods and how to use them in PHP ?.

Related : Magic Methods, __set() Method, __isset() Method, Magic Constants, Magic Methods, Magic Property, Property Hook