Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a trick for you, to pass variables by references via __call magic method: I don't now how long will this works or in witch php versions. I tested on php 5.3.2</p> <p>Fitst you cannot pass $arguments variable by $reference in __call function definition. Because php falls with error.</p> <p>So first: Here is a code what does not do, what you want with referenced arguments, but nearly good ;)</p> <pre><code> class A { public $x = array(); } class Teszt{ private function addElement( &amp;$list ){ $list-&gt;x[] = 'new element'; return count($list-&gt;x); } public function __call($name,$arguments){ if (!preg_match('#^add([0-9]+)Element$#', $name, $matches) || $matches[1]&lt;1){ trigger_error ("Function is not exists teszt::$name", E_USER_ERROR); } $ret = null; for($i=0;$i&lt;$matches[1];$i++){ $ret = call_user_func_array(array($this,'addElement'), $arguments); } return $ret; } } $a = new A(); $a-&gt;x = array('old element','old element'); $t = new Teszt(); $cnt = $t-&gt;add5Element($a); var_dump($a); var_dump($cnt); </code></pre> <p>OUTPUT:</p> <pre><code> PHP Warning: Parameter 1 to Teszt::addElement() expected to be a reference, value given in /home/gkovacs/callMagicWithReference.php on line 19 PHP Stack trace: PHP 1. {main}() /home/gkovacs/callMagicWithReference.php:0 PHP 2. Teszt-&gt;add2Element() /home/gkovacs/callMagicWithReference.php:27 PHP 3. Teszt-&gt;__call() /home/gkovacs/callMagicWithReference.php:0 PHP 4. call_user_func_array() /home/gkovacs/callMagicWithReference.php:19 PHP Warning: Parameter 1 to Teszt::addElement() expected to be a reference, value given in /home/gkovacs/callMagicWithReference.php on line 19 PHP Stack trace: PHP 1. {main}() /home/gkovacs/callMagicWithReference.php:0 PHP 2. Teszt-&gt;add2Element() /home/gkovacs/callMagicWithReference.php:27 PHP 3. Teszt-&gt;__call() /home/gkovacs/callMagicWithReference.php:0 PHP 4. call_user_func_array() /home/gkovacs/callMagicWithReference.php:19 object(A)#1 (1) { ["x"]=&gt; array(2) { [0]=&gt; string(11) "old element" [1]=&gt; string(11) "old element" } } NULL </code></pre> <p>ohhh :((( After a litle 'HACK':</p> <pre><code> class A { public $x = array(); } class Teszt{ private function addElement( &amp;$list ){ $list-&gt;x[] = 'new element'; return count($list-&gt;x); } public function __call($name,$arguments){ if (!preg_match('#^add([0-9]+)Element$#', $name, $matches) || $matches[1]&lt;1){ trigger_error ("Function is not exists teszt::$name", E_USER_ERROR); } $ret = null; //Look at my hand, because i will cheat foreach($arguments as $k=&gt;&amp;$v){ } //end of cheat for($i=0;$i&lt;$matches[1];$i++){ $ret = call_user_func_array(array($this,'addElement'), $arguments); } return $ret; } } $a = new A(); $a-&gt;x = array('old element','old element'); $t = new Teszt(); $cnt = $t-&gt;add5Element($a); var_dump($a); var_dump($cnt); </code></pre> <p>Output:</p> <pre><code>object(A)#1 (1) { ["x"]=&gt; array(4) { [0]=&gt; string(11) "old element" [1]=&gt; string(11) "old element" [2]=&gt; string(11) "new element" [3]=&gt; string(11) "new element" } } int(4) </code></pre> <p>As we wanted. This works only with objects, but not with array-s. There is a way with array-s to call-time-pass-by-reference ... like this: (and of corse you can use this way with objects too)</p> <pre><code> $cnt = $t-&gt;add5Element(&amp;$a); </code></pre> <p>In this case php generate notices ... </p> <p>The easiest way to redefine the function if it is possible. in my code , : private functionaddElement($list) , don't define as reference in parameter list. And as you read in previous message , it will works beacause objects are passed by reference automatically But sometimes , you can not redefine functions because of implemented interfaces or other reasons ... </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload