Page 1 of 1

Weirness with $this pointer and arguments passed by referenc

Posted: Tue Apr 29, 2008 6:46 am
by TheKog
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


This issue cropped up in a CakePHP app but I think our problem is more related to something we don't understand about PHP. It seems invocation ofvia $this of a method in an associated class does not properly handle arguments by reference.

In a Bait controller class (BaitsController) a function foo from the Bait model class is invoked - $this is properly instantiated:

Code: Select all

 
function test() 
{ 
Bait::foo($xxx); // works correctly 
$this->Bait->foo($xxx); // does not work 
}
In Bait model class function foo() takes the parm by ref and sets it's value:

Code: Select all

function foo(&$msg) 
{ 
$msg = "HOWDY DOODY"; 
}
ERROR is Undefined variable: xxx ...

If you put function foo within the Bait controller class $this->foo() works fine. However if you put it in the model Class using the $this->Bait->foo() invocation syntax seems to ignore the parameter by reference although it does call the function. What's up with that?


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.