note 53719 added to migration5.oop
| From: | hairmare-spam at purplehaze dot ch | Date: | Fri, 10 Jun 2005 11:41:15 +0000 |
| Subject: | note 53719 added to migration5.oop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90222@lists.php.net to get a copy of this message | ||
I have noted some discussion going on about the 'Cannot re-assign $this' error.
The most simple way to reproduce the error is:
class MyObject
{
function test()
{
$this = new MyObject;
}
}
I found the following quote (quoting zend.com) on php-dev:
http://www.zend.com/php/ask_experts.php :
"The fact that you could change in PHP 4, was never really meant to be
although it worked. In PHP 5 we removed this option due to performance
and semantic reasons. In PHP 5, you can either use a factory method
pattern or throw an exception on error in the constructor."
This statement implies that the error lies not in PHP5 but in the script. A Bogus Bug in http://bugs.php.net/bug.php?id=27659 tells me the
same thing.
There has been some discussion on how to migrate such code to php5 without having to reengineer the
whole App. From my point of view the following solutions are viable:
1. Use delegation & Object composition:
$this->Object = new Whatever();
function nameOfFunction()
{
return $this->Object->nameOfFunction();
}
This requires lots of code rewriting. Some of it could be realised with overloading. Variables would
need to get overloaded or could only be accessed using a getter/setter. As a xtra you get clean OO
Code!
2. Replace Original Objects Content:
foreach (get_object_vars($foo) as $key => $value)
$this->$key = $value;
This solution only works with instance-variables. Additional classkit/runkit code would be needed to
get everything else correct. This way less refactoring would be needed. This could be the right way
to go if you don't need to replace any methods or the classname.
I haven't decided wich way to go. I wrote this so the next hacker with this problem finds some
info where I was looking for it.
----
Manual Page -- http://www.php.net/manual/en/migration5.oop.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53719
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53719&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53719&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53719&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53719&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53719&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53719&report=yes
Search -- http://master.php.net/manage/user-notes.php