|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-06-30 05:12 UTC] muhamad_zakaria at yahoo dot com
Description:
------------
When we used virtual variables exploiting __set overload method, we encountered errors.
Reproduce code:
---------------
class TheObj {
public $RealVar1, $RealVar2, $RealVar3, $RealVar4;
public $Var = array();
function __set($var, $val) {
$this->Var[$var] = $val;
}
function __get($var) {
if(isset($this->Var[$var])) return $this->Var[$var];
else return -1;
}
}
$SomeObj = new TheObj;
// this will fine
$SomeObj->RealVar1 = 'somevalue';
$SomeObj->{'RealVar2'} = 'othervalue';
$SomeObj->{'RealVar'.(3)} = 'othervaluetoo';
$SomeObj->{'RealVar'.'4'} = 'anothervalue';
// this will fine too
$SomeObj->Virtual1 = 'somevalue';
$SomeObj->{'Virtual2'} = 'othervalue';
// it's can't be used since this will encounter error
$SomeObj->{'Virtual'.(3)} = 'othervaluetoo';
$SomeObj->{'Virtual'.'4'} = 'anothervalue';
// but this will fine, ofcourse
$SomeObj->Var['Virtual'.(3)] = 'othervaluetoo';
$SomeObj->Var['Virtual'.'4'] = 'anothervalue';
Expected result:
----------------
No error when we use below lines:
<?php
$SomeObj->{'Virtual'.(3)} = 'othervaluetoo';
$SomeObj->{'Virtual'.'4'} = 'anothervalue';
?>
because this should applied fine as we did at "RealVarX" treatments.
Actual result:
--------------
Encountered error raises by php.exe
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 22:00:01 2025 UTC |
We have tried from the feedback (tony2001), and the raised error is no longer. But we have another experiences while using 'unset' statement, such as below: // we will try to unset these variables unset($SomeObj->RealVar1); unset($SomeObj->{'RealVar'.(3)}); //the lines below will catch by '__get' magic method since these variables are unavailable anymore print $SomeObj->RealVar1."\n"; print $SomeObj->{'RealVar'.(3)}."\n"; // now we will try to unset these variables unset($SomeObj->Virtual1); unset($SomeObj->{'Virtual'.(3)}); //but, these variables are still available??? eventhough they're "unset"-ed print $SomeObj->Virtual1."\n"; print $SomeObj->{'Virtual'.(3)}."\n";