{"issue":{"state":"open","pull_request":{"diff_url":null,"html_url":null,"patch_url":null},"user":{"avatar_url":"https://secure.gravatar.com/avatar/0235f1841a524eaa497edd39d825f885?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"0235f1841a524eaa497edd39d825f885","url":"https://api.github.com/users/suhana","login":"suhana","id":167591},"milestone":null,"assignee":null,"updated_at":"2011-11-07T14:52:02Z","labels":[],"comments":1,"closed_at":null,"url":"https://api.github.com/repos/fabpot/Twig/issues/506","body":"I'm not sure how to explain this fully, but it has become a nasty stumbling block for me; that is the inablity to access special properties that are only available via the __get() magic method.\r\n\r\nI have been using a simplified model that only fetchs data if and when it is needed - (see below for an example). As it stands, Twig is unable to see the properties normally accessed in this way as uses Reflection to examine the class directly.\r\n\r\nWith small models, this is not a problem, however the largel models accesing tables with many fields and deep references to other tables this becomes a task I cannot see how to elegantly overcome using Twig in its current form.\r\n\r\n\r\n```php\r\n<?php\r\n/**\r\n  * CREATE TABLE users\r\n  * (\r\n  *     id      INT UNSIGNED  NOT NULL,\r\n  *     name    VARCHAR(20)   NOT NULL,\r\n  *     born    INT UNSIGNED  NOT NULL,\r\n  *     gender  ENUM('M','F') NOT NULL,\r\n  *     partner INT UNSIGNED  NOT NULL DEFAULT '0',\r\n  *\r\n  *     PRIMARY KEY(id)\r\n  * );\r\n  *\r\n  * INSERT INTO users (id, name, born, gender, partner)\r\n  *    VALUES\r\n  *       (1, 'Alice', 329821200, 'F', 2),\r\n  *       (2, 'Bob',   289422295, 'M', 1);\r\n  **/\r\nclass User\r\n{\r\n    private static $instance;\r\n    public  $id;\r\n    private $data = null;\r\n    \r\n    public static function instance( $id )\r\n    {\r\n        if (!isset(self::$instance))\r\n            self::$instance = array();\r\n            \r\n        if (!array_key_exists($id, self::$instance))\r\n            self::$instance[$id] = new User($id);\r\n            \r\n        return self::$instance[$id];\r\n    }\r\n    \r\n    public function __construct( $id )\r\n    {\r\n        $this->id = $id;\r\n    }\r\n    \r\n    public function __get( $key )\r\n    {\r\n        if (is_null($_data))\r\n        {\r\n            // data hase not yet been loaded,\r\n            // load from the database\r\n            \r\n            $rs  = mysql_query('SELECT * FROM users WHERE id = ' . $this->id);\r\n            $row = mysql_fetch_array($rs, MYSQL_ASSOC);\r\n            \r\n            $this->data = $row;\r\n            \r\n            mysql_free_result($rs);\r\n        }\r\n        \r\n        // check for calculated fields and\r\n        // special fields returning objects\r\n        \r\n        switch ($key)\r\n        {\r\n            case 'age':\r\n                return time() - $this->data['born'];\r\n                \r\n            case 'partner':\r\n                // Lazy loading of partner only occurss *if* the\r\n                // calling process actually needs access to it.\r\n                if ($this->data['partner'])\r\n                    return User::instance($this->data['partner'];\r\n                return null;\r\n        }\r\n        \r\n        if (array_key_exists($key, $this->data))\r\n            return $this->data[$key];\r\n            \r\n        throw new Exception('Property \"' . $key . '\" not found');\r\n    }\r\n}\r\n\r\n$loader = new Twig_Loader_String();\r\n$twig   = new Twig_Environment($loader);\r\n$user   = User::instance(1);\r\n$html   = <<<EOF\r\n    <dl>\r\n        <dt>ID</dt>\r\n        <dd>{{ user.name }}</dd>\r\n        <dt>Name</dt>\r\n        <dd>{{ user.name }}</dd>\r\n    </dl>\r\nEOF;\r\necho $twig->render\r\n(\r\n    $html,\r\n    array\r\n    (\r\n        'user' => $user\r\n    )\r\n);\r\n\r\n/**\r\n  * Ideal Result\r\n  * ============\r\n  * <dl>\r\n  *     <dt>ID</dt>\r\n  *     <dd>1</dd>\r\n  *     <dt>Name</dt>\r\n  *     <dd>Susan</dt>\r\n  * </dl>\r\n  *\r\n  *\r\n  * Actual Result\r\n  * =============\r\n  * <dl>\r\n  *     <dt>ID</dt>\r\n  *     <dd></dd>\r\n  *     <dt>Name</dt>\r\n  *     <dd></dt>\r\n  * </dl>\r\n  **/\r\n```\r\n","html_url":"https://github.com/fabpot/Twig/issues/506","title":"Inability to access custom properties","number":506,"id":2162926,"created_at":"2011-11-07T14:15:54Z"},"action":"created","comment":{"user":{"avatar_url":"https://secure.gravatar.com/avatar/b4ec98df44deb80b1576283152670358?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png","gravatar_id":"b4ec98df44deb80b1576283152670358","url":"https://api.github.com/users/nikic","login":"nikic","id":216080},"updated_at":"2011-11-07T14:52:02Z","url":"https://api.github.com/repos/fabpot/Twig/issues/comments/2654286","body":"Twig can handle magic. Could you try to define an `__isset` method and see if it works?","id":2654286,"created_at":"2011-11-07T14:52:02Z"}}