-
Notifications
You must be signed in to change notification settings - Fork 27
Magic __get method fails for data() #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
When it checks to see if the method exists the check doesn't validate magic methods. I remember when this was removed a while ago. I will see why I requested that support be removed. |
I grabbed a copy of the new-parser branch and ran my sample above against it. The magic get of property foo via If I change the Interestingly, that also means I can now call |
Correct. With the new parser, function calls must be explicit. |
Thank you for the responses. I'd like to see the new parser merged to master. :) |
Would still like to see PHP magic methods work, too. |
I think this may have caused an issue with maphper earlier with magic methods being called at the wrong time |
I think the problem before was that since functions weren't explicit it would call properties |
This test passes public function testDataObjCallMagicMethod() {
$xml = "
<div></div>
";
$data = new Foo;
$tss = "div { content: data(testCall()); }";
$template = new \Transphporm\Builder($xml, $tss);
$this->assertEquals('<div>test</div>', $template->output($data)->body);
}
class Foo {
public $model;
public function __construct() {
$this->model = new Bar();
}
public function getBar($bar) {
return $bar;
}
public function returnFalse() {
return false;
}
public function returnTrue() {
return true;
}
public function returnOne() {
return 1;
}
public function add($a, $b) {
return $a+$b;
}
public function __call($name, $args) {
if ($name == "testCall") return "test";
throw new \Exception();
}
} |
I assume this can be closed |
I'm not completely sure. What's the proper syntax to access an object's public property under the new parser? That is, if I have: class Foo {
public $bar = "bar";
}
$data = new Foo; How do I write TSS to ask for |
If foo is being passed as root data then |
Does not work with magic method require 'vendor/autoload.php';
class getTest {
private $data = array('a' => 'a', 1 => 1);
public function __get($name) {
echo " >> Getting property '$name'\n";
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
throw new Exception("no such property");
return null;
}
}
$data = new getTest;
$xml = "<div></div>\n";
$tss = "div { content: data(__get('a')); }"; // this works
$tss = "div { content: data(__get(1)); }"; // this works
$tss = "div { content: data(a); }"; // fails
$tss = "div { content: data(1); }"; // fails
$template = new \Transphporm\Builder($xml, $tss);
echo $template->output($data)->body; |
The reason |
Ah! Thanks! Adding an public function __isset($name) {
if (array_key_exists($name, $this->data)) {
return true;
}
return false;
} |
I just added |
This should definitely be documented to avoid future confusing. |
I added some light documentation |
Uh oh!
There was an error while loading. Please reload this page.
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):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 viadata(__get)
but don't know how to pass the argument.The text was updated successfully, but these errors were encountered: