By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

cannot re-assign $this ...

P: n/a
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!!

daniel

Feb 20 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Daniel,

given the note of Wolverine on
http://nl2.php.net/manual/en/language.oop.php
it seems like assinging $this will cause the assigned value to be
returned. When you replace
$this = func_get_arg(0);
return;
by
return func_get_arg(0);

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

greetings,

Henk Verhoeven,
www.pphPeanuts.org.

Daniel Loose wrote:
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!!

daniel
Feb 20 '07 #2

P: n/a
On Tue, 20 Feb 2007 23:15:09 +0100, Henk Verhoeven
<ne***@phppeanutsREMOVE-THIS.orgwrote:
When you replace
$this = func_get_arg(0);
return;
by
return func_get_arg(0);

you shoud get the same results in php4 and it should work in php5 too.
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,
daniel

Feb 21 '07 #3

P: n/a
Rik
Daniel Loose wrote:
Henk Verhoeven wrote:
>When you replace
$this = func_get_arg(0);
return;
by
return func_get_arg(0);

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

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... :-)
Well, unfortunately, I have no solution for you. Then again, I'm not that
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 function
instead:

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 needed?
Seems illogical...
--
Rik Wasmus
Feb 21 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.