note 66348 added to migration5.oop
| From: | quinn at strangecode dot com | Date: | Fri, 19 May 2006 15:53:42 +0000 |
| Subject: | note 66348 added to migration5.oop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-111690@lists.php.net to get a copy of this message | ||
Here is another possible solution for migrating code to php 5 when using $this =
'something' reassignments. In my case, I had several classes with methods that were
self-instantiating with static calls. I was able to simply use a different variable: I changed $this
to $_this and it worked the same because I copied an instance of the original object by reference
using an instantiation factory method:
class DB {
function &getInstance()
{
static $instance = null;
if ($instance === null) {
$instance = new DB();
}
return $instance;
}
...
In every method needing access to this object I assigned it to a temporary variable by reference:
function doSomething ()
{
$_this =& DB::getInstance();
$_this->doSomethingElse();
$_this->param['id'] = 123;
}
Which allows method calls or saving data back to the original object.
I originally created classes like this so I didn't need to keep track of instantiations or
global objects. I could just call DB::doSomething() and the object is created dynamically or
referenced from an already existing object.
----
Server IP: 64.71.164.2
Probable Submitter: 63.195.16.136
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/migration5.oop.php
Edit -- http://master.php.net/note/edit/66348
Del: integrated -- http://master.php.net/note/delete/66348/integrated
Del: useless -- http://master.php.net/note/delete/66348/useless
Del: bad code -- http://master.php.net/note/delete/66348/bad+code
Del: spam -- http://master.php.net/note/delete/66348/spam
Del: non-english -- http://master.php.net/note/delete/66348/non-english
Del: in docs -- http://master.php.net/note/delete/66348/in+docs
Del: other reasons-- http://master.php.net/note/delete/66348
Reject -- http://master.php.net/note/reject/66348
Search -- http://master.php.net/manage/user-notes.php