php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #21300 Problem with variable variables using classes
Submitted: 2002-12-30 19:15 UTC Modified: 2002-12-30 20:01 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: zoinky at zoinky dot 2y dot net Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 4.2.3 OS: Windows
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:
MUST BE VALID
Solve the problem:
38 + 29 = ?
Subscribe to this entry?

 
 [2002-12-30 19:15 UTC] zoinky at zoinky dot 2y dot net
It appears that when you are using classes, there are problems when using variable variables.

In this instance, I have a class called Account.  In it there are variables named $uid, $handle, $password, and so on.  In my MySQL database, there are column names of uid, handle, password, etc.  When I perform a query, I attempt to store the results in my class, using this code:

$arr = mysql_fetch_assoc($result);
foreach ($arr as $key => $value)
{
	$this->$$key = $value;
}

When I use this, I received warnings along the likes of:

Warning: Undefined variable: handle in e:\httpserv\zoinky\wba\sources\accountclass.php on line 51

and respective warnings for each variable/column.  It should be noted that if I try to read the values, they return 0, or an empty string.

I have verified this on my computer (Windows XP Pro, Apache 1.3.26, PHP 4.2.3 as an Apache module), as well as on my webhost (Windows 2000 Server, Xitami 2.4d9, PHP 4.1.0 as a CGI).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-12-30 20:01 UTC] pollita@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

Recall that accessing properties of a class is done as so:

$this->uid = 1234;

therefore if $key = "uid", then $this->$$key would come out like "$this->$uid" which is invalid.

Try using:
  $this->$key = $value;
In the loop you provided in your example.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Dec 04 09:03:37 2021 UTC