php.net |  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
 [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'].";}";
}
eval($str);

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-02-06 17:38 UTC] waldschrott@php.net
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: Thu Apr 18 12:01:28 2024 UTC