473,382 Members | 1,814 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,382 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 1752
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.