cannot re-assign $this ...

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
dear folks,

I'm one more guy having the cannot re-assign $this problem.

someone left a php4 file here containing the lines

|    function linkbar()
|    {
|        if(func_num_args() == 1)
|        {

|            $this = func_get_arg(0);
|            return;
|    }

inside a php4 class definition. func_get_arg(0) in this case delivers
an object - working fine in php4. I struggled a bit with the &
referencing (while not really understanding it and the problem) but
gave up. how must I modify the code to get it to work in php5?

thank you very much!!


Re: cannot re-assign $this ...

Hi Daniel,

given the note of Wolverine on
it seems like assinging $this will cause the assigned value to be  
returned. When you replace
            $this = func_get_arg(0);
            return func_get_arg(0);

you shoud get the same results in php4 and it should work in php5 too.


Henk Verhoeven,

Daniel Loose wrote:
Quoted text here. Click to load it

Re: cannot re-assign $this ...

On Tue, 20 Feb 2007 23:15:09 +0100, Henk Verhoeven

Quoted text here. Click to load it

hm, thank you hank, however this is not the solution. of course it
removes the cannot reassign error msg, but gives another errror in
another line that wants to access the object (by using $this again)
("Call to a member function initLink() on a non-object ") I'm not an
expert here but I don't think that return func... does the same as the
original code... :-)

hoping for any other ideas,  

Re: cannot re-assign $this ...

Daniel Loose wrote:
Quoted text here. Click to load it

Well, unfortunately, I have no solution for you. Then again, I'm not tha=
t  =

well versed in OO, but this seems Bad Practise to me. A hidious, hidous,=

hidous quick fix could be, if the object is a global, to use this functi=
on  =


function evil_switch_object($name,$object){
    $GLOBALS[$name] = $object;

Then again, I can not find the logic to switch WHY this is not done like=

$foo = new foo();//first class/object
$bar = new bar();//second class/object
$foo = $bar;//instead of $foo->linkbar($bar);

Could you go into further detail as to why this exact construct is neede=
d?  =

Seems illogical...
-- =

Rik Wasmus

Site Timeline