Skip to content

Magic __get method fails for data() #88

@cxj

Description

@cxj

Accessing object properties via the magic __get() method does not work. Note that no exception is thrown, hence I don't think it is even called. This might be a PHP syntax special case, such as described in this block part way down the PHP Overloading page (php.net/__get):

Note:
The return value of __set() is ignored because of the way PHP processes the assignment operator. Similarly, __get() is never called when chaining assignments together like this:
$a = $obj->b = 8;

Can this be made to work? Is there a variation on calling data() in the TSS that can make it work in the current code? I tried a few variations, and can call __get explicitly via data(__get) but don't know how to pass the argument.

<?php
require_once "vendor/autoload.php";

$xml = '<h1>Original Title</h1>';

class Data {
    protected $data = array();
    public    $baz = 'Baz';

    public function __construct() {
        $this->data['foo'] = 'Foo';
        $this->data['bar'] = 'Bar';
    }

    public function __get($name) {
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }
        throw new Exception("no such property");
    }

    public function getBar() {
        return $this->data['bar'];
    }
}

$output = new Data;

// this fails, using __get magic method.
$tss      = 'h1 {content: data(foo); }';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output($output)->body . PHP_EOL;

// this works, using explicit method.
$tss      = 'h1 {content: data(getBar); }';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output($output)->body . PHP_EOL;

// this works, using public property.
$tss      = 'h1 {content: data(baz); }';
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output($output)->body . PHP_EOL;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions