Connecting Tech Pros Worldwide Forums | Help | Site Map

I need $this->self to be identical to and reference to $this, but how in PHP 5.2+?

comp.lang.php
Guest
 
Posts: n/a
#1: Jan 8 '07
[PHP]
<?
class EditResultGenerator extends MethodGeneratorForActionPerformer {

/**
* Create an instance of itself if $this does not yet exist as an
EditResultGenerator class object
*
* @access private
* @see is_class
*/
function &generateSelf() { // STATIC VOID METHOD
if (!is_object($this) || @!is_class($this, 'EditResultGenerator')) {
$this->self =& new EditResultGenerator();
} else {
$this->self =& $this;
}
}

}
?>
[/PHP]

This self-generating function serves a purpose to ensure that if part
of the application does this:

[PHP]
<?
EditResultGenerator::getResult();
?>
[/PHP]

That it will work *exactly* the same as if I did this:

[PHP]
<?
$edit =& new EditResultGenerator();
$result = $edit->getResult();
?>
[/PHP]

However, I cannot change hundreds of lines of code from the former to
the latter just to ensure instantiation is uniform, so I'm stuck with
the fact that I have to generate a $this->self EditResultGenerator
class object if $this does not exist, otherwise, $this->self must be
*absolutely identical* to $this, in fact, be a reference!

This works beautifully in PHP 4.3+, but bombs in PHP 5.2.

How can I get it to work in PHP 5.2+?

Phil


hackajar
Guest
 
Posts: n/a
#2: Jan 8 '07

re: I need $this->self to be identical to and reference to $this, but how in PHP 5.2+?


http://us3.php.net/manual/en/language.oop5.basic.php
Check out example 19-4 & 19-5

Hackajar
comp.lang.php wrote:
Quote:
[PHP]
<?
class EditResultGenerator extends MethodGeneratorForActionPerformer {
>
/**
* Create an instance of itself if $this does not yet exist as an
EditResultGenerator class object
*
* @access private
* @see is_class
*/
function &generateSelf() { // STATIC VOID METHOD
if (!is_object($this) || @!is_class($this, 'EditResultGenerator')) {
$this->self =& new EditResultGenerator();
} else {
$this->self =& $this;
}
}
>
}
?>
[/PHP]
>
This self-generating function serves a purpose to ensure that if part
of the application does this:
>
[PHP]
<?
EditResultGenerator::getResult();
?>
[/PHP]
>
That it will work *exactly* the same as if I did this:
>
[PHP]
<?
$edit =& new EditResultGenerator();
$result = $edit->getResult();
?>
[/PHP]
>
However, I cannot change hundreds of lines of code from the former to
the latter just to ensure instantiation is uniform, so I'm stuck with
the fact that I have to generate a $this->self EditResultGenerator
class object if $this does not exist, otherwise, $this->self must be
*absolutely identical* to $this, in fact, be a reference!
>
This works beautifully in PHP 4.3+, but bombs in PHP 5.2.
>
How can I get it to work in PHP 5.2+?
>
Phil
comp.lang.php
Guest
 
Posts: n/a
#3: Jan 8 '07

re: I need $this->self to be identical to and reference to $this, but how in PHP 5.2+?



hackajar wrote:
Quote:
http://us3.php.net/manual/en/language.oop5.basic.php
Check out example 19-4 & 19-5
Ok, but I don't get it. I understand assignment properties and
instantiation but I guess not enough to see how that pertains to what
I'm doing here.
Quote:
>
Hackajar
comp.lang.php wrote:
Quote:
[PHP]
<?
class EditResultGenerator extends MethodGeneratorForActionPerformer {

/**
* Create an instance of itself if $this does not yet exist as an
EditResultGenerator class object
*
* @access private
* @see is_class
*/
function &generateSelf() { // STATIC VOID METHOD
if (!is_object($this) || @!is_class($this, 'EditResultGenerator')) {
$this->self =& new EditResultGenerator();
} else {
$this->self =& $this;
}
}

}
?>
[/PHP]

This self-generating function serves a purpose to ensure that if part
of the application does this:

[PHP]
<?
EditResultGenerator::getResult();
?>
[/PHP]

That it will work *exactly* the same as if I did this:

[PHP]
<?
$edit =& new EditResultGenerator();
$result = $edit->getResult();
?>
[/PHP]

However, I cannot change hundreds of lines of code from the former to
the latter just to ensure instantiation is uniform, so I'm stuck with
the fact that I have to generate a $this->self EditResultGenerator
class object if $this does not exist, otherwise, $this->self must be
*absolutely identical* to $this, in fact, be a reference!

This works beautifully in PHP 4.3+, but bombs in PHP 5.2.

How can I get it to work in PHP 5.2+?

Phil
Hendri Kurniawan
Guest
 
Posts: n/a
#4: Jan 8 '07

re: I need $this->self to be identical to and reference to $this, but how in PHP 5.2+?


comp.lang.php wrote:
Quote:
[PHP]
<?
class EditResultGenerator extends MethodGeneratorForActionPerformer {
>
/**
* Create an instance of itself if $this does not yet exist as an
EditResultGenerator class object
*
* @access private
* @see is_class
*/
function &generateSelf() { // STATIC VOID METHOD
if (!is_object($this) || @!is_class($this, 'EditResultGenerator')) {
$this->self =& new EditResultGenerator();
} else {
$this->self =& $this;
}
}
>
}
?>
[/PHP]
>
This self-generating function serves a purpose to ensure that if part
of the application does this:
>
[PHP]
<?
EditResultGenerator::getResult();
?>
[/PHP]
>
That it will work *exactly* the same as if I did this:
>
[PHP]
<?
$edit =& new EditResultGenerator();
$result = $edit->getResult();
?>
[/PHP]
>
However, I cannot change hundreds of lines of code from the former to
the latter just to ensure instantiation is uniform, so I'm stuck with
the fact that I have to generate a $this->self EditResultGenerator
class object if $this does not exist, otherwise, $this->self must be
*absolutely identical* to $this, in fact, be a reference!
>
This works beautifully in PHP 4.3+, but bombs in PHP 5.2.
>
How can I get it to work in PHP 5.2+?
>
Phil
>

Singleton... I like... :)

<?php
function & generateSelf() {
static $self = null;
if(is_null($self)) $self = new EditResultGenerator();
return $self;
}
?>


Hendri Kurniawan
Hendri Kurniawan
Guest
 
Posts: n/a
#5: Jan 8 '07

re: I need $this->self to be identical to and reference to $this, but how in PHP 5.2+?


Quote:
>
>
Singleton... I like... :)
>
<?php
function & generateSelf() {
static $self = null;
if(is_null($self)) $self = new EditResultGenerator();
return $self;
}
?>
>
>
Hendri Kurniawan

Sorry (forgot static keyword in front of function

<?php
static function & generateSelf() {
static $self = null;
if(is_null($self)) $self = new EditResultGenerator();
return $self;
}
?>

Hendri Kurniawan
comp.lang.php
Guest
 
Posts: n/a
#6: Jan 8 '07

re: I need $this->self to be identical to and reference to $this, but how in PHP 5.2+?



Hendri Kurniawan wrote:
Quote:
Quote:


Singleton... I like... :)

<?php
function & generateSelf() {
static $self = null;
if(is_null($self)) $self = new EditResultGenerator();
return $self;
}
?>


Hendri Kurniawan
>
>
Sorry (forgot static keyword in front of function
Actually, I can't use the "static keyword" in front of the function.
Requirements are that it must be compatible all the way back to PHP
4.1.2, so that's out, sorry.

I'll give the rest a try, of course.
Quote:
>
<?php
static function & generateSelf() {
static $self = null;
if(is_null($self)) $self = new EditResultGenerator();
return $self;
}
?>
>
Hendri Kurniawan
Toby Inkster
Guest
 
Posts: n/a
#7: Jan 8 '07

re: I need $this->self to be identical to and reference to $this, but how in PHP 5.2+?


comp.lang.php wrote:
Quote:
However, I cannot change hundreds of lines of code from the former to
the latter
Why not? Haven't you heard of search and replace?

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Koncept
Guest
 
Posts: n/a
#8: Jan 9 '07

re: I need $this->self to be identical to and reference to $this, but how in PHP 5.2+?


In article <1168250213.972762.77460@s80g2000cwa.googlegroups. com>,
comp.lang.php <phillip.s.powell@gmail.comwrote:
Quote:
[PHP]
<?
class EditResultGenerator extends MethodGeneratorForActionPerformer {
>
/**
* Create an instance of itself if $this does not yet exist as an
EditResultGenerator class object
*
* @access private
* @see is_class
*/
function &generateSelf() { // STATIC VOID METHOD
if (!is_object($this) || @!is_class($this, 'EditResultGenerator')) {
$this->self =& new EditResultGenerator();
} else {
$this->self =& $this;
}
}
>
}
?>
[/PHP]
>
This self-generating function serves a purpose to ensure that if part
of the application does this:
>
[PHP]
<?
EditResultGenerator::getResult();
?>
[/PHP]
>
That it will work *exactly* the same as if I did this:
>
[PHP]
<?
$edit =& new EditResultGenerator();
$result = $edit->getResult();
?>
[/PHP]
>
However, I cannot change hundreds of lines of code from the former to
the latter just to ensure instantiation is uniform, so I'm stuck with
the fact that I have to generate a $this->self EditResultGenerator
class object if $this does not exist, otherwise, $this->self must be
*absolutely identical* to $this, in fact, be a reference!
>
This works beautifully in PHP 4.3+, but bombs in PHP 5.2.
>
How can I get it to work in PHP 5.2+?
>
Phil
>

For a PHP5 implementation, I wouldn't modify within
EditResultGenerator directly per sae. I would add a Singleton class and
then reference that as needed. For example..

<?php
class Singleton{
private static $instances = array();
private function __construct(){}
public function getInstance($class){
if(!array_key_exists($class, self::$instances)){
// PHP5 creates by reference "=&" automatically
self::$instances[$class] = new $class;
}
return self::$instances[$class];
}
public final function __clone(){
trigger_error( "Cannot clone Singleton class", E_USER_ERROR );
}
}

// Modified this class as a demo...
class MethodGeneratorForActionPerformer{
private $signature;
function __construct(){
$this->signature = rand();
}
function getUnique(){
echo "Unique signature is ", $this->signature,"\n";
}
}

class EditResultGenerator extends MethodGeneratorForActionPerformer {
// your other methods...
}

// Demo for getting single instance
$edit1 = Singleton::getInstance('EditResultGenerator');
$edit2 = Singleton::getInstance('EditResultGenerator');

// Test that they are the same
$edit1->getUnique();
$edit2->getUnique();
?>

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Closed Thread