Page 1 of 1

Creating functions in classes - why does this not work? :/

Posted: Thu Oct 01, 2009 10:34 am
by jackpf
Hi all,
I'm trying to create functions within classes "on the fly", but I can't get it to work.

Here's my "test":

Code: Select all

<?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??
?>
Which doesn't work.

However, this works:

Code: Select all

 
<?php
$bar = function()
{
    echo 'hello world';
};
 
$bar(); //works
?>
Does anyone know why I can't do this? It works for normal functions...shouldn't it work for methods too? Or am I doing it wrong? :/

Thanks for any help,
Regards,
Jack.

Re: Creating functions in classes - why does this not work? :/

Posted: Thu Oct 01, 2009 12:22 pm
by requinix

Code: Select all

$foo->bar();
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

Code: Select all

$func = $foo->bar;
$func();

Re: Creating functions in classes - why does this not work? :/

Posted: Thu Oct 01, 2009 12:55 pm
by jackpf
Oh right.

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...

However, the __get() function is never executed.

Code: Select all

<?php
class foo
{
    private $bar = 'hello';
}
 
$foo = new foo;
$foo->__get = function()
{
    echo 'accessed';
};
 
echo $foo->bar; //error - inaccessible property
?>
Any ideas? :/

Cheers,
Jack.

Re: Creating functions in classes - why does this not work? :/

Posted: Thu Oct 01, 2009 1:08 pm
by requinix
You can't do it that way. If you want a function you have to define it normally.

Code: Select all

<?php
class foo
{
    public function __get()
    {
        return 'accessed';
    }
}
$foo = new foo;
echo $foo->bar;
?>

Re: Creating functions in classes - why does this not work? :/

Posted: Thu Oct 01, 2009 1:10 pm
by jackpf
Yeah, but I'm using the stdclass...so I can't really define functions normally.

Ahh well. I guess this isn't really possible :/

Cheers for your help tasairis,
Jack.

Re: Creating functions in classes - why does this not work? :/

Posted: Thu Oct 01, 2009 1:30 pm
by requinix
Not possible... the way you're trying to do it.

Like I said: use __call().

Re: Creating functions in classes - why does this not work? :/

Posted: Thu Oct 01, 2009 1:57 pm
by jackpf
But...I can't :P

To do that I'd have to define the __call() function...and to do that I need the __call() function.

Re: Creating functions in classes - why does this not work? :/

Posted: Thu Oct 01, 2009 2:13 pm
by requinix
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.

Re: Creating functions in classes - why does this not work? :/

Posted: Thu Oct 01, 2009 3:40 pm
by Eran
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).

Re: Creating functions in classes - why does this not work? :/

Posted: Thu Oct 01, 2009 3:41 pm
by jackpf
Yeah...I guess.

I just can't really be bothered to make a class specifically for template variables.

I'll mull it over :P

Cheers for the help,
Jack.