[PATCH][Discussion] Ensure property name a valid label and properly handle array2object conversion&Req for bugfix review.
| From: | reeze | Date: | Sun, 08 Apr 2012 16:38:20 +0000 |
| Subject: | [PATCH][Discussion] Ensure property name a valid label and properly handle array2object conversion&Req for bugfix review. | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-59467@lists.php.net to get a copy of this message | ||
Hi, folks:
Recently I have been trying to fix bugs on bugs.php.net. I've been looking at the bug: https://bugs.php.net/bug.php?id=61655.
for short: after convert a stdClass with a property with number name to array can't be access
anymore.
after some code searching, I found that. class property lookup differs from array's. when
convert the
internal HashTable won't be touched. class property always lookup by string, but array will try
to handle
numeric(@see Zend/zend_hash.h:307 ZEND_HANDLE_NUMERIC macro).
I don't know whether this has been discussed before, if so forgive me please.
now PHP allow to access property with this syntax: $obj->{$expr} $expr can be anything. PHP
didn't try to validate whether it is a valid label or not. the only problem is we can't
access it directly
like: $expr = '007gun'; $obj->{$expr} = 10; echo $obj->007gun; It's not a big
problem, we can still access
it with the same syntax. but after convert to array, we got problem.
On the contrary array to object conversion have the same problem:
<?php
$ar = array(0 => "value", "000invlidlabel" => "2", key"
=> "value2");
$obj = (object)$ar;
$obj->0 will never be accessible, "000invilidlabel" can be access, but can't use
normal property access syntax.
I know PHP make use of HashTable a lot, maybe because of performance concern or historical reason.
but we always want to be better.
In my opinion:
1. I see no reason why user will set an invalid label as property name. because It can be access
with $obj->{$expr} only or sometime can never been accessed.
2. for the same reason array to object should at least let developer know something they may not
want to happen did happened.
so I make some change:
1. set a property name should be valid label(__set magic method also follow it)
2. raise warning and move invlid property names to a special property when convert a array contains
invlid key name to stdClass.
I just want to make things more consist, or maybe we just make sure numeric keys works consist since
php is so flexible.
Any thoughts?
BTW: I sent bugfix for bug#61347 ArrayIterator misrewind and gives misleading notice when
manipulating empty/moved to end array
https://github.com/php/php-src/pull/38 thanks
@nikic & @cataphract's great suggestions, will someone take a look, do I still have to
improve it?
Thank you!
--
reeze | reeze.cn