Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Johannes Schlüter:
References - Still bad in PHP 7
Feb 19, 2016 @ 15:18:45

Johannes Schlüter has a post to his site that talks about references in PHP 7 and how they're "still bad" based on some of his previous findings.

I'm known for telling "Don't use references" (also as video) as those cause different problems (i.e. with foreach) and hurt performance. The reason for the performance loss is that references disable copy-on-write while most places in PHP assume copy-on-write. Meanwhile we have PHP 7. In PHP 7 the internal variable handling changed a lot among other things the reference counting moved from the zval, the container representing a variable, to the actual element. So I decided to run a little test to verify my performance assumption was still valid.

He includes his testing code that calls a function (strlen) in a loop and compares the handling against two methods, one passing by reference the other not. The results are shown in time taken to execute. He compares the results for PHP 5 and PHP 7, noting that PHP 7 is marginally better when passed by value, by-reference is still about the same.

tagged: reference php7 php5 compare value byreference byvalue test benchmark execution

Link: http://schlueters.de/blog/archives/180-References-Still-bad-in-PHP-7.html

Nikita Popov:
The case against the ifsetor function
Jan 13, 2014 @ 15:22:52

In his latest post Nikita Popov aims to make a case against the introduction of the "ifsetor" function to be introduced into the PHP language. This function takes in a variable to find and, if found returns it. If not, it doesn't produce an error (or warning).

Recently igorw wrote a blog post on how to traverse nested array structures with potentially non-existing keys without throwing notices. The current “idiomatic” way to do something like this, is to use isset() together with a ternary operator. [...] Someone on /r/PHP pointed out that there is an alternative approach to this problem, namely the use of an ifsetor function.

He goes on to talk about by-reference argument passing, why requesting an undefined array index doesn't really throw an error and how writes don't have the same issues as reads. He then gets into his own issues around the "ifsetor" function, namely:

  • Creation of dummy values
  • No notices for nested indices
  • Null values treated as non-existing
  • Default is always evaluated
  • By-reference passing often forces a copy

He summarizes most of the issues in one statement - "there is way too much by-ref magic involved". He then looks at some of the ways that this could be helped but opts instead for something more like "get_in" as proposed by Igor.

tagged: ifsetor getin array read write problem byreference

Link: http://nikic.github.io/2014/01/10/The-case-against-the-ifsetor-function.html


Trending Topics: