473,773 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning a null reference in PHP 4.4.2

Consider a class that encapsulates an associative array. It has two
"setter" methods:

class A
{
....
function setAttribute( $name, $value )
{
$this->attributes[ $name ] = $value;
}
....
function setAttributeByR ef( $name, &$value )
{
$this->attributes[ $name ] =& $value;
}
....

The following "getter" method *used* to work just fine prior to version
4.4.2:

....
function & getAttribute( $name )
{
if ( isset( $this->attributes[ $name ] ) )
return $this->attributes[ $name ];
return NULL;
}
....

However, as of version 4.4.2, this throws a "FATAL: Only variable
references should be returned by reference." error. Unfortunately, my
host (and several of my client hosts) are stuck on this version.

Is there an elegant way to solve this problem? I want to return a value
indicating that no such attribute exists.

(I've googled the problem and can't find anything useful other than a
suggestion in a PHP bug report that I learn how to write "proper"
code.)

Thanks!

J

Aug 3 '06 #1
4 1911
ja**********@gm ail.com wrote:
function & getAttribute( $name )
{
if ( isset( $this->attributes[ $name ] ) )
return $this->attributes[ $name ];
return NULL;
}
...

However, as of version 4.4.2, this throws a "FATAL: Only variable
references should be returned by reference." error. Unfortunately, my
host (and several of my client hosts) are stuck on this version.

Is there an elegant way to solve this problem? I want to return a value
indicating that no such attribute exists.
I think that a better solution would be to work with the array keys.

But doesn't this work:

function & getAttribute($n ame)
{
return $this->attributes[$name];
}

For an inelegant hack:
function &getAttribute($ name)
{
static $dummy;
$dummy=NULL;
return (isset($this->attributes[$name]) ?
$this->attributes[$name]
: $dummy);
}

C.
Aug 3 '06 #2
Colin McKinnon wrote:
But doesn't this work:

function & getAttribute($n ame)
{
return $this->attributes[$name];
}
Hmmm. I could have swore that threw an error too if the key wasn't
set... Am I thinking an older version? (Another language?)

That seems to work. I'll have to re-work a large chunk of code, but
that seems to do it. Thanks. :)

J

Aug 3 '06 #3
Hi,

To be honest, i think the "solution" of php4 to reference anomalies
sucks. I suggest you use php5, or php 4.3. In php4.4 you have to resort
to this kind of ugly code:

function &getAttribut e( $name )
{
if ( isset( $this->attributes[ $name ] ) )
return $this->attributes[ $name ];
$null = NULL;
return $null;
}

BTW, you may not need $this->attributes, you can do:

function setAttribute( $name, $value )
{
$this->$name = $value;
}
function &getAttribut e( $name )
{
if ( isset( $this->$name ) )
return $this->$name;
$null = NULL;
return $null;
}

Greetings,

Henk Verhoeven.
More info about reference anomalies:
http://www.phppeanuts.org/site/index...e+anomaly.html
ja**********@gm ail.com wrote:
Consider a class that encapsulates an associative array. It has two
"setter" methods:

class A
{
...
function setAttribute( $name, $value )
{
$this->attributes[ $name ] = $value;
}
...
function setAttributeByR ef( $name, &$value )
{
$this->attributes[ $name ] =& $value;
}
...

The following "getter" method *used* to work just fine prior to version
4.4.2:

...
function & getAttribute( $name )
{
if ( isset( $this->attributes[ $name ] ) )
return $this->attributes[ $name ];
return NULL;
}
...

However, as of version 4.4.2, this throws a "FATAL: Only variable
references should be returned by reference." error. Unfortunately, my
host (and several of my client hosts) are stuck on this version.

Is there an elegant way to solve this problem? I want to return a value
indicating that no such attribute exists.

(I've googled the problem and can't find anything useful other than a
suggestion in a PHP bug report that I learn how to write "proper"
code.)

Thanks!

J
Aug 3 '06 #4
ja**********@gm ail.com wrote:
function & getAttribute( $name )
{
if ( isset( $this->attributes[ $name ] ) )
return $this->attributes[ $name ];
return NULL;
}
Why not just

function &getAttribut e( $name )
{
return $this->attributes[ $name ];
}

Aug 3 '06 #5

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

Similar topics

0
1444
by: {RainmakeR} | last post by:
Hi all, I've noticed something strange with returning references, both in PHP4 and 5 - maybe it's supposed to do this, I don't know - but it seems a bit weird nonetheless. Say we have the following test code: <?php class Test {
1
2521
by: John Hunter | last post by:
I am writing a python extension module and have a reference counting question My function looks like static PyObject * pokereval_seven_cards(PyObject *self, PyObject *args) { int i;
7
20375
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But , for example, when the file doesnt exists, i should not return any reference to a bad constructed object, so i need something as a NULL reference object. I suppose i could return a pointer instead, but i have some code written with references which...
19
1985
by: JKop | last post by:
When I compile and run the following on my system: #include <iostream> static int hello = 78; int ReturnValue(void) {
5
3107
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the function below is returning a pointer to pointers who are GUID. I am trying to write a wrapper to use in my VB code and what I would prefer to do is be able to return an array of GUID. I remember (not sure) that the concept of arrays does not really...
11
1944
by: JKop | last post by:
AnyClass Blah() { AnyClass poo; return poo; } As we all know, in the above, the compiler is entitled to:
1
5224
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote peer. I have no problem connecting the peers and streaming Network Streams. When the incoming data is finished recieving, I act upon it. This works great as long as all of the code is inside my form. I want to build the networking code into a...
21
2699
by: Jim Langston | last post by:
I'm sure this has been asked a few times, but I'm still not sure. I want to create a function to simplify getting a reference to a CMap in a map. This is what I do now in code: std::map<unsigned int, CMap*>::iterator ThisMapIt = World.Maps.find( ThisPlayer.Character.Map ); if ( ThisMapIt != World.Maps.end() )
23
2941
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or method. QUOTE ENDS HERE I have never heard anyone else say that it is a problem for a function
8
2220
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
0
9621
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
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
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
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
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
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
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.