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

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!

Moderator: General Moderators

Post Reply
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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();
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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;
?>
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

Not possible... the way you're trying to do it.

Like I said: use __call().
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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).
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

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