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
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: aj at homeboyz dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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: Sat Dec 21 15:01:29 2024 UTC