php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51099 Variable Variable with $this->
Submitted: 2010-02-20 19:18 UTC Modified: 2010-02-20 19:25 UTC
From: William dot McGuire at solacedev dot com Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 5.3.1 OS: Win XP
Private report: No CVE-ID: None
 [2010-02-20 19:18 UTC] William dot McGuire at solacedev dot com
Description:
------------
Consider 2 classes

class user
class reports extends user

user stores public variables such as public $username using $this->username;

I want to be able to dynamically say in class reports

echo $this->$field where $field = "username", "first_name" etc.

Reproduce code:
---------------
---
From manual page: http://www.php.net/language.variables.variable
---
class user {
public $username;

function username() { 
//queries from db table and gets
$this->username = $username; //from table
}

class reports extends user {
function userInfo($gid,$arrFieldstoReport) {
parent::__construct($gid)
//loop through $arrFieldstoReport
echo "username = " . $this->arrFieldstoReport[$i]
}
}

Expected result:
----------------
This should echo the following if $arrFieldstoReport[$i] = "username"

echo $this->arrFieldstoReport[$i];
//should echo 
username = testusername (or any other username depending on what group_id it is on)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-02-20 19:22 UTC] William dot McGuire at solacedev dot com
Just to let you know dynamically populating $this-> works fine...


ie...
$this->$db_key=$db_value where $db_key is username and $db_value is "testuser"  works fine...

it's just dynamically echoing $this->$field where $field = "username"
 [2010-02-20 19:25 UTC] William dot McGuire at solacedev dot com
I figured out the problem...it is not a bug...thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Feb 27 05:01:32 2024 UTC