php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33652 $this not available in a variable variable
Submitted: 2005-07-12 03:06 UTC Modified: 2005-07-12 11:32 UTC
From: ludvig dot ericson at gmail dot com Assigned: dmitry (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5CVS-2005-07-11 OS: *
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ludvig dot ericson at gmail dot com
New email:
PHP Version: OS:

 

 [2005-07-12 03:06 UTC] ludvig dot ericson at gmail dot com
Description:
------------
Using variable variables ($$MyVar) in classes, through functions that is, doesn't allow $this to be used.

I tried with $this (notice the $) too, no difference.
A workaround is to check the object name, if it's "this" you could $this->$Mem instead.

Reproduce code:
---------------
<?php
class Foo {
    public $MyVal = "Test";

    public Bar($Obj, $Mem) {
        return ${$Obj}->$Mem;
    }
}

$TheObject = new Foo();
$Objname = "this";
$Memname = "MyVal";
echo $TheObject->Bar($Objname, $Memname);
?>

Expected result:
----------------
I expect it to output "Test"

Actual result:
--------------
Notice: Undefined variable: this in (filepath) on line 6
Notice: Trying to get property of non-object in (filepath) on line 6

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-12 03:30 UTC] colder@php.net
Notice also that ${'this'} will work but not ${'this'.''} or ${(string)'this'} for example.

It should be at least documented that this pseudo-variable lacks some features.
 [2005-07-12 10:20 UTC] sniper@php.net
Works fine with PHP 4.4.0, not with latest CVS HEAD.

 [2005-07-12 11:32 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Mar 15 02:01:30 2025 UTC