|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-04-23 14:09 UTC] agostone at freemail dot hu
Description:
------------
If i have an overloaded __set function in a class and call parent::__set from another function, the parent calls the child's __set back and a loop gets created like this:
parent::__set (initial call)
child::_set
parent::_set
I need __set function to do some prefixing by default when assigning new string properties by default, i need a custom function to assign un-prefixed properties too, if there is a need (will happen very rarely)... I need this to auto prefix string variables passed to my view object (they gotta be passed as object properties), the MVC framework is a 3rd party framework, i'm extending it's view object and i don't want to modify the original source...
Reproduce code:
---------------
<?php
class A
{
public function __set($pKey, $pValue)
{
echo("Class: ".get_class()."| Passed Value:".$pValue."<BR>");
$this->$pKey = $pValue;
return true;
}
}
class B extends A
{
function setRaw($pKey, $pValue)
{
parent::__set($pKey, $pValue);
}
function __set($pKey, $pValue)
{
echo("Class: ".get_class()."| Passed Value:".$pValue."<BR>");
$pValue = $this->prefix($pValue);
parent::__set($pKey, $pValue);
}
function prefix($pValue)
{
if(is_string($pValue))
{
return "(prefixed) ".$pValue;
}
return $pValue;
}
}
$b = new B();
echo("Calling: setRaw()<br>");
$b->setRaw("raw", "shouldn't be prefixed");
echo("<br>");
echo("Calling: __set()<br>");
$b->prefixed = "should be prefixed";
echo("<br>Variable after setraw: ".$b->raw);
echo("<br>Variable after set: ".$b->prefixed);
echo("<br>");
?>
Expected result:
----------------
Calling: setRaw()
Class: A| Passed Value:shouldn't be prefixed
Calling: __set()
Class: B| Passed Value:should be prefixed
Class: A| Passed Value:(prefixed) should be prefixed
Variable after setRaw: shouldn't be prefixed
Variable after set: (prefixed) should be prefixed
Actual result:
--------------
Calling: setRaw()
Class: A| Passed Value:shouldn't be prefixed
Class: B| Passed Value:shouldn't be prefixed
Class: A| Passed Value:(prefixed) shouldn't be prefixed
Calling: __set()
Class: B| Passed Value:should be prefixed
Class: A| Passed Value:(prefixed) should be prefixed
Variable after setraw: (prefixed) shouldn't be prefixed
Variable after set: (prefixed) should be prefixed
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 23:00:01 2025 UTC |
This is "expected", the recursion protection that allows you to dynamically create properties inside __set is not enabled before the first __set called via overloading. Example: class A { public function __set($n, $v) { echo "Setting $n = $v\n"; $this->$n = $v; } } $a = new A; $a->k1 = "v1"; $a->__set("k2", "v2"); Will end up outputting: Setting k1 = v1 Setting k2 = v2 Setting k2 = v2 because the recursion protection isn't there when you do a manual call to __set().