|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #9137 eval adds function to global space rather than class method
Submitted: 2001-02-06 17:32 UTC Modified: 2001-02-06 17:38 UTC
From: aj at homeboyz dot com Assigned: waldschrott (profile)
Status: Closed Package: Class/Object related
PHP Version: 4.0.4pl1 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
43 + 31 = ?
Subscribe to this entry?

 [2001-02-06 17:32 UTC] aj at homeboyz dot 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'].";}";


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2001-02-06 17:38 UTC]
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
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Feb 26 09:01:26 2024 UTC