473,467 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Create 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 setAttributeByRef( $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 1895
ja**********@gmail.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($name)
{
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($name)
{
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 &getAttribute( $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 &getAttribute( $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**********@gmail.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 setAttributeByRef( $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**********@gmail.com wrote:
function & getAttribute( $name )
{
if ( isset( $this->attributes[ $name ] ) )
return $this->attributes[ $name ];
return NULL;
}
Why not just

function &getAttribute( $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
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...
1
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
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 ,...
19
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
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...
11
by: JKop | last post by:
AnyClass Blah() { AnyClass poo; return poo; } As we all know, in the above, the compiler is entitled to:
1
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...
21
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: ...
23
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...
8
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
0
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...
0
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,...
0
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.