You are here: Re: Returning a null reference in PHP 4.4.2 « PHP Programming Language « IT news, forums, messages
Re: Returning a null reference in PHP 4.4.2

Posted by Henk Verhoeven on 08/03/06 21:14

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_php/Pagina/167/reference+anomaly.html


jamiesonhale@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
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация