开发者

PHP 5.4's simplified string offset reading

As many of you already know, PHP 5.4 alpha has been released. I have a question regarding the following.

Simplified string offset reading. $str[1][0] is now a legal construct.

How exactly does $str[1][0] work?

E开发者_开发知识库DIT: http://php.net/releases/NEWS_5_4_0_alpha1.txt


It just means that when reading a string offset PHP returns a string again, on which you again can access an offset. (And on that access yet another offset. It gets funny with $str[0][0][0][0][0][0])

Before PHP 5.4 you would get an "Cannot use string offset as an array" error.


This is a side effect, and was mentioned in the proposal here: http://php.markmail.org/thread/yiujwve6zdw37tpv

The feature is speed/optimization of string offsets.

Hi,

Recently I noticed that reading of string offset is performed in two steps. At first special string_offset variant of temporary_variable is created in zend_fetch_dimension_address_read() and then the real string value is created in _get_zval_ptr_var_string_offset().

I think we can create the real string in the first place. This makes 50% speed-up on string offset reading operation and allows to eliminate some checks and conditional brunches in VM.

The patch is attached (don't forget to regenerate zend_vm_execute.h to test it). However it changes behavior in one bogus case. The following code now will emit "b" (currently it generates a fatal error - cannot use string offset as an array).

$str = "abs";
var_dump($str[1][0]);

I think it's not a problem at all. "b" makes sense because "abs"[1] -> "b" and "b"[0] -> "b".

I'm going to commit the patch in case of no objections.

Thanks. Dmitry.


This can actually create some interesting bugs when you upgrade code from php 5.3 to 5.4.

In 5.3 this construct would return false:

$array = array("This is a string");

echo isset($array[0][0][0]);

In 5.4 this would return true.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜