473,804 Members | 4,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

[PHP]
<?
class EditResultGener ator extends MethodGenerator ForActionPerfor mer {

/**
* Create an instance of itself if $this does not yet exist as an
EditResultGener ator class object
*
* @access private
* @see is_class
*/
function &generateSel f() { // STATIC VOID METHOD
if (!is_object($th is) || @!is_class($thi s, 'EditResultGene rator')) {
$this->self =& new EditResultGener ator();
} else {
$this->self =& $this;
}
}

}
?>
[/PHP]

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

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

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

[PHP]
<?
$edit =& new EditResultGener ator();
$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 EditResultGener ator
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 1778
http://us3.php.net/manual/en/language.oop5.basic.php
Check out example 19-4 & 19-5

Hackajar
comp.lang.php wrote:
[PHP]
<?
class EditResultGener ator extends MethodGenerator ForActionPerfor mer {

/**
* Create an instance of itself if $this does not yet exist as an
EditResultGener ator class object
*
* @access private
* @see is_class
*/
function &generateSel f() { // STATIC VOID METHOD
if (!is_object($th is) || @!is_class($thi s, 'EditResultGene rator')) {
$this->self =& new EditResultGener ator();
} else {
$this->self =& $this;
}
}

}
?>
[/PHP]

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

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

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

[PHP]
<?
$edit =& new EditResultGener ator();
$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 EditResultGener ator
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 EditResultGener ator extends MethodGenerator ForActionPerfor mer {

/**
* Create an instance of itself if $this does not yet exist as an
EditResultGener ator class object
*
* @access private
* @see is_class
*/
function &generateSel f() { // STATIC VOID METHOD
if (!is_object($th is) || @!is_class($thi s, 'EditResultGene rator')) {
$this->self =& new EditResultGener ator();
} else {
$this->self =& $this;
}
}

}
?>
[/PHP]

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

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

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

[PHP]
<?
$edit =& new EditResultGener ator();
$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 EditResultGener ator
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 EditResultGener ator extends MethodGenerator ForActionPerfor mer {

/**
* Create an instance of itself if $this does not yet exist as an
EditResultGener ator class object
*
* @access private
* @see is_class
*/
function &generateSel f() { // STATIC VOID METHOD
if (!is_object($th is) || @!is_class($thi s, 'EditResultGene rator')) {
$this->self =& new EditResultGener ator();
} else {
$this->self =& $this;
}
}

}
?>
[/PHP]

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

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

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

[PHP]
<?
$edit =& new EditResultGener ator();
$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 EditResultGener ator
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($sel f)) $self = new EditResultGener ator();
return $self;
}
?>
Hendri Kurniawan
Jan 8 '07 #4
>

Singleton... I like... :)

<?php
function & generateSelf() {
static $self = null;
if(is_null($sel f)) $self = new EditResultGener ator();
return $self;
}
?>
Hendri Kurniawan

Sorry (forgot static keyword in front of function

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

Hendri Kurniawan
Jan 8 '07 #5

Hendri Kurniawan wrote:


Singleton... I like... :)

<?php
function & generateSelf() {
static $self = null;
if(is_null($sel f)) $self = new EditResultGener ator();
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($sel f)) $self = new EditResultGener ator();
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************ *********@s80g2 000cwa.googlegr oups.com>,
comp.lang.php <ph************ **@gmail.comwro te:
[PHP]
<?
class EditResultGener ator extends MethodGenerator ForActionPerfor mer {

/**
* Create an instance of itself if $this does not yet exist as an
EditResultGener ator class object
*
* @access private
* @see is_class
*/
function &generateSel f() { // STATIC VOID METHOD
if (!is_object($th is) || @!is_class($thi s, 'EditResultGene rator')) {
$this->self =& new EditResultGener ator();
} else {
$this->self =& $this;
}
}

}
?>
[/PHP]

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

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

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

[PHP]
<?
$edit =& new EditResultGener ator();
$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 EditResultGener ator
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
EditResultGener ator 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($cl ass){
if(!array_key_e xists($class, self::$instance s)){
// PHP5 creates by reference "=&" automatically
self::$instance s[$class] = new $class;
}
return self::$instance s[$class];
}
public final function __clone(){
trigger_error( "Cannot clone Singleton class", E_USER_ERROR );
}
}

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

class EditResultGener ator extends MethodGenerator ForActionPerfor mer {
// your other methods...
}

// Demo for getting single instance
$edit1 = Singleton::getI nstance('EditRe sultGenerator') ;
$edit2 = Singleton::getI nstance('EditRe sultGenerator') ;

// 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
524
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 manager of an employee.This ReportsTo column is basically the EmployeeID column: EmployeeID Title ReportsTo FirstName SecondName ...... ...... 1 Manager 4 Steven Buchan
6
1368
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
1477
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 frustratingly slow. Creating and viewing thumbnails for a 50-page tiff (scan of text document) takes about 40 seconds. Commercial image viewers do the same thing in a split second. I read on bobpowell.net that for fast GDI+, you need DirectX...
0
1263
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 supported. I must be losing something somewhere, but the byte lengths match up. Is there some encoding I need to do. Please take a look, and suggestions would be appreciated.
0
1170
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 can get the text of that label in the other column, in order to create a querystring on the end of the redirect - - I just can't figure out how to find the current row, and then find the label inside the other column, in the same row can...
4
2655
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, how do I reset the ISS to see the new data? Does this make sense?
10
1318
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 any of you have references for that type of article? Thanks.
1
1304
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
1342
by: vicksa | last post by:
when to use this,this.value, this.property name and whats the definition and meaning of 'this' ?
1
1321
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 difference between the this reference in C++ and 'this' in PHP?
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.