class - php Set a anonymous function in an instance -


I'm just starting with PHP, and I'm wondering if adding an unknown function to a class instance .

For example, we say ...

  class A {public B; } $ C = new A (); // This is where I am getting a little confused ... // do not do the following work $ c-> B = function () {echo ('HelloWorld');}; $ C & gt; B ();  

What I'm hoping to do, spit like code is used in many different applications, and make it so that I just 'swap out' Can I change the function and in particular the examples

I am using php5.3 (hence, anonymous functions should work, just the way I am using them).

Thank you very much for your time!

- GK

You can use magic function for this job . Not a beauty, but it works ..

Like this:

  class A {public $ B; Public function __coll ($ ending, $ arg) {call_user_func_array ($ this-> $ off, $ args); }} $ C = new A (); $ C- & gt; B = function () {echo ('helowwald'); }; $ C & gt; B ();  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -