Re: [PHP] PHP 4.3.11, call_user_func and instances of classes
On Wed, April 26, 2006 6:46 pm, David Otton wrote: > class Test { > var $x; > function Test () > { > global $addition, $subtraction; > $this->x = 0; > $addition = array ($this, 'AddOne'); > $subtraction = array ($this, 'SubtractOne'); Unsolicited Advice: You MIGHT also want to consider using $this->addition and $this->subtraction instead of global variables. Just a bit cleaner, I think. -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 4.3.11, call_user_func and instances of classes
The problem is not what it seems. PHP4 assigns object by copy, not by reference. This is causing the call_user_func() to use a copy of the object instead of the original object. So, all modifications are lost once the call is done. One solution to this is to assign objects by reference: $addition = array (&$this, 'AddOne'); $subtraction = array (&$this, 'SubtractOne'); 2006/4/26, David Otton <[EMAIL PROTECTED]>: > > A bit of an oddity, this. There's some example code attached which > illustrates my problem. > > I am attempting to call a method of an instance of an class from > outside that instance, using call_user_func(). > > What's happening is that my attempt to call > > array ($this, 'AddOne') > > is silently being rewritten into a call to > > array ('Test', 'Addone') > > in other words, instead of calling $test->AddOne I'm calling > Test::Addone. Thus, my expected output: > > Add:1,Add:2,Add:3,Subtract:2,Subtract:1,Subtract:0, > > becomes > > Add:1,Add:1,Add:1,Subtract:-1,Subtract:-1,Subtract:-1, > > So, my question is twofold: > > a) How can I accomplish this? > > b) Why is PHP silently modifying my code to mean something I didn't > write, rather than throwing up an error? > > $addition = $subtraction = null; > > class Test { > var $x; > function Test () > { > global $addition, $subtraction; > $this->x = 0; > $addition = array ($this, 'AddOne'); > $subtraction = array ($this, 'SubtractOne'); > doMath('+'); doMath('+'); doMath('+'); > doMath('-'); doMath('-'); doMath('-'); > } > function AddOne () > { > $this->x++; > echo ("Add:".$this->x.","); > } > function SubtractOne () > { > $this->x--; > echo ("Subtract:".$this->x.","); > } > } > > function doMath($choice) > { > global $addition, $subtraction; > switch ($choice) > { > case '+': > call_user_func ($addition); > break; > case '-': > call_user_func ($subtraction); > break; > } > } > > $test = new Test(); > ?> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
[PHP] PHP 4.3.11, call_user_func and instances of classes
A bit of an oddity, this. There's some example code attached which illustrates my problem. I am attempting to call a method of an instance of an class from outside that instance, using call_user_func(). What's happening is that my attempt to call array ($this, 'AddOne') is silently being rewritten into a call to array ('Test', 'Addone') in other words, instead of calling $test->AddOne I'm calling Test::Addone. Thus, my expected output: Add:1,Add:2,Add:3,Subtract:2,Subtract:1,Subtract:0, becomes Add:1,Add:1,Add:1,Subtract:-1,Subtract:-1,Subtract:-1, So, my question is twofold: a) How can I accomplish this? b) Why is PHP silently modifying my code to mean something I didn't write, rather than throwing up an error? x = 0; $addition = array ($this, 'AddOne'); $subtraction = array ($this, 'SubtractOne'); doMath('+'); doMath('+'); doMath('+'); doMath('-'); doMath('-'); doMath('-'); } function AddOne () { $this->x++; echo ("Add:".$this->x.","); } function SubtractOne () { $this->x--; echo ("Subtract:".$this->x.","); } } function doMath($choice) { global $addition, $subtraction; switch ($choice) { case '+': call_user_func ($addition); break; case '-': call_user_func ($subtraction); break; } } $test = new Test(); ?> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php