PHP 4.0 Bug #9137 Updated: eval adds function to global space rather than class method

From: Date: Tue, 06 Feb 2001 22:38:58 +0000
Subject: PHP 4.0 Bug #9137 Updated: eval adds function to global space rather than class method
Groups: php.dev 
Request: Send a blank email to php-dev+get-45884@lists.php.net to get a copy of this message
ID: 9137 Updated by: waldschrott Reported By: aj@homeboyz.com Old-Status: Open Status: Closed Bug Type: Class/Object related Assigned To: waldschrott Comments: and this is correct, eval uses its own scope, the only possibilty I see at the moment to solve your specific problem is to eval() the entire class maybe in combination with inheritance Previous Comments: --------------------------------------------------------------------------- [2001-02-06 17:32:47] aj@homeboyz.com I am trying to generate class methods on the fly. I created a string which was php code for a function. I wished this funciton to be a class method. I call eval on the string, and it produces no errors. When I wish to call that method of the object, the script fails indicating a call to an undefined funciton. However, I can call that function in the page space, rather than as a class method. But, the function is useless unless it is a method. The following is part of the code that is executed in the constructor: $str = ""; foreach($this->getMappedAttributes() as $attribute) { $str .= "$"."this->_".$attribute['attribute']." = "";"; $str .= "function get".$attribute['attribute']."() {"; $str .= "return $"."this->_".$attribute['attribute'].";}"; } eval($str); --------------------------------------------------------------------------- ATTENTION! Do NOT reply to this email! To reply, use the web interface found at http://bugs.php.net/?id=9137&edit=2

« previous php.dev (#45884) next »