php Set a anonymous function in an instance

Posted by geekay on Stack Overflow See other posts from Stack Overflow or by geekay
Published on 2010-06-13T21:50:44Z Indexed on 2010/06/13 21:52 UTC
Read the original article Hit count: 331

Filed under:
|
|
|

I am just starting out with PHP, and I am wondering if there is a way to add an anonymous function to a class instance.

For instance, lets say...

class A{
    public B;
}

$c = new A();

//This is where I am getting a little confused...
//The following wont work

$c->B = function(){echo('HelloWorld');};
$c->B();

What I am hoping to do is reuse the same spit of code in a great number of different applications, and make it so that I can just 'swap-out' and replace functions in specific instances.

I am using php5.3 (so anonymous functions should work, just not in the way that I am using them).

Thanks so very much for your time!!

-GK

© Stack Overflow or respective owner

Related posts about php

Related posts about class