Re: Dynamic Class Methods

From: Date: Tue, 07 Sep 2004 15:55:56 +0000
Subject: Re: Dynamic Class Methods
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-196045@lists.php.net to get a copy of this message
* Thus wrote Mathieu Dumoulin: > I'm trying to setup a function from user code inside a defined object or a > class definition but it doesnt seem to work. Am i doing it right? Is there > something i need to know... is it even possible? > > I know create_function can be used to create a function and it returns the > ident to run the function later on, lookup create_function on php.net and it > seems possible. Now i wonder if you can actually assign this function inside > a class. My final goal is to create a set of functions or objects that will > be able to "implement" interfaces to object classes, which is something > missing to the PHP language. > > ------------------ > RESULT > ------------------ > allo > Fatal error: Call to undefined function: b() in > /home/tech/web/testboard/implements.php on line 21 This is because $a->b is a member not a method. One option that you might want to look at is: http://php.net/classkit Keep in mind the above is still in beta, but will allow you to add/remove/redefine methods of objects at runtime. iirc it works with php4 and php5. The other option would be to set a variable within a class that the method b() uses: class impl_a { var $_b; function setFunc($func, $params, $code) { $var = "_$func"; /* set up $this->varname */ $this->$var = create_function($params, $code); } function b() { $func = $this->_b; $func(); } } $a = new impl_a(); $a->setFunc('b', '', 'echo "hello world"'); $a->b(); Its Not very clean solution, but might work in you situation. Curt -- The above comments may offend you. flame at will.

« previous php.general (#196045) next »