473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

[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

Jan 8 '07 #1
7 1749
http://us3.php.net/manual/en/language.oop5.basic.php
Check out example 19-4 & 19-5

Hackajar
comp.lang.php wrote:
[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
Jan 8 '07 #2

hackajar wrote:
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.
>
Hackajar
comp.lang.php wrote:
[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
Jan 8 '07 #3
comp.lang.php wrote:
[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
Jan 8 '07 #4
>

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
Jan 8 '07 #5

Hendri Kurniawan wrote:


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.
>
<?php
static function & generateSelf() {
static $self = null;
if(is_null($self)) $self = new EditResultGenerator();
return $self;
}
?>

Hendri Kurniawan
Jan 8 '07 #6
comp.lang.php wrote:
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

Jan 8 '07 #7
In article <11*********************@s80g2000cwa.googlegroups. com>,
comp.lang.php <ph**************@gmail.comwrote:
[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
Jan 9 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: al | last post by:
Greetings, Would be thankful for your kind help. I have a table (Northwind employees table). In the employees table, a column called ReportsTo which lists the EmployeeID, int data type, of the...
6
by: greenflame | last post by:
I need code to compute the determinant and inverse of a matrix. for example the determinant and inverse of:
3
by: johnb41 | last post by:
I am building a .net app that does some simple image tasks (Tiff files): viewing Tiff, viewing thumbnails from multipage tiff, rotation, page (frame) deletion, etc. The problem is that it is so...
0
by: kid4rilla | last post by:
I can successfully write the binary data to an image data type, and successfully retrieve it, but when I attempt to play the file in media player after retrieving it, I get the file type isn't...
0
by: Elmo Watson | last post by:
I have a linkbutton in one gridview column - I also have another column with a label on it - lblID the linkbutton has a redirect, but I need a reference to the current row it's on (VB.Net), so I...
4
by: clb | last post by:
I am trying to use stringstreams and my book doesn't cover the included methods. For example, if I init a istringstream on a string and suck all the data out, then put more stuff into the string,...
10
by: not_a_commie | last post by:
I've seen studies before showing that it is better to rewrite code when more than 25% (or whatever) of the code needs to be changed. I can't seem to locate any references for that at the moment. Do...
1
by: Debby Dadebo | last post by:
Hello, Please I need a program code that will convert roman numerals to integers. I will be glad if you can add the programs algorithm and flowchart. Thanks and God Bless you
1
by: vicksa | last post by:
when to use this,this.value, this.property name and whats the definition and meaning of 'this' ?
1
by: yasmine | last post by:
hi friends, May i know what is the purpose of the 'this' in php? Is it pointer or reference? eg: $this -> filename = $obj -> filename; $this -> filepath = $obj -> filepath; Is there any...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.