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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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

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: Tue Mar 05 05:01:29 2024 UTC