PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
class foo
{
public $bar;
}
$foo = new foo;
$foo->bar = function()
{
echo 'hello world';
};
$foo->bar(); //returns undefined function error
var_dump($foo); //it says bar is "object(Closure)#2"...shouldn't it be a function??
?>
With that syntax, PHP will go looking for a member function in foo called "bar". There isn't one. What you did was take a member variable and create a function out of it.
If you need this kind of magic functionality, use __call. Otherwise
Well, what I'm doing is for my templates, is I pass each template function its vars as an argument in the form of an stdclass object.
What I was hoping to do is somehow check that each variable the template tries to access exists. I thought that setting up a __get() function within the stdclass that checks each variable exists would be the best way to do it...
Don't use a stdClass for it. I mean, if you want to use a class you might as well use a class; say, a TemplateVars.
Otherwise all you're accomplishing is making the system more complicated with no gain.
Instead of a template function, use a templates class. You can then access its $this scope for additional functionality. That's the common way to handle this (read about View classes).