Rasmus Lerdorf wrote:
So while the tone of some of these messages may not be great,
> often the suggestion that the code in question isn't very good is
accurate.

Rasmus, I think where some of the concern and confusion lies is with those of us who have been doing, or at least attempting, some "heavy" OOP with PHP4. Now, PHP4 may not be the most ideal platform for OOP (hence the changes PHP5 brings), but nevertheless it *is* there, it is available to use, and despite it's shortcomings, use it we did.

Many of us who did some crazy OOP in PHP4 have used references heavily because, like in PHP5, we wanted to avoid making copies of objects everywhere. Hence, a lot of code returns by reference and assigns by reference. At the time, I certainly didn't think I was writing "bad" code - it seemed to make sense, and PHP4 happily ran it as expected without error or complaint. Code like this, for example:

function &getProcessor()
{
   return $this->processor->getInstance();
}

...where the processor attribute is a dynamically-configured object which may generate a different object instance (from getInstance() call) depending on it's configuration. Now in 4.4, this of course generates a notice, and has to be changed to:

function &getProcessor()
{
   $instance =& $this->processor->getInstance();

   return $instance;
}

...which to me just seems like a waste of space. This raises other questions, for example, what if we're trying to return something by reference and encounter a problem with returning that something? For example, take some sort of collection or list class:

function &at($index=NULL)
{
   if ($this->hasIndex($index))
   {
      return $this->elements[$index];
   }

   return NULL;
}

...you want to grab an element at the specified index by reference if it's an object. The above code used to work fine, but now of course it generates a notice, and needs to be changed to something like:

function &at($index=NULL)
{
   $element = NULL;

   if ($this->hasIndex($index))
   {
      $element =& $this->elements[$index];
   }

   return $element;
}

These things by themselves are not a big deal, but many of us have these "little" issues happening throughout our code, resulting in hundreds upon hundreds of notices per page request. Perhaps according to the true nature of references these examples were not the ideal way to code, but I don't consider it "bad code", and it was certainly legal and fully-functional code for some time now. Call it awful if you like, but it certainly made sense to me at the time.

Regards,

Colin.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to