Returning a null reference in PHP 4.4.2

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jamiesonhale@gmail.com

    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

  • Colin McKinnon

    #2
    Re: Returning a null reference in PHP 4.4.2

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

    Comment

    • jamiesonhale@gmail.com

      #3
      Re: Returning a null reference in PHP 4.4.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

      Comment

      • Henk Verhoeven

        #4
        Re: Returning a null reference in PHP 4.4.2

        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:



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

        Comment

        • Chung Leong

          #5
          Re: Returning a null reference in PHP 4.4.2

          jamiesonhale@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 ];
          }

          Comment

          Working...