|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 17 19:00:01 2025 UTC |
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.