Objects as Arrays

php.internals

(Marcus Börger)

22 years ago
Hello internals, IIRC it was intended to use object properties as arrays. Currently most array_*() functions do explicitly check for IS_ARRAY. The patch below now enables objects in nearly all array_*() functions. The contray of such an approach is that it would allow to access/modify protected and private properties. But for some functions we do it already so we need to find a solution for that sooner or later independantly from this patch. Any thoughts? http://marcus-boerger.de/php/ext/standard/php-array-20030918.diff.txt
-- Best regards, Marcus mailto:mail@marcus-boerger.de

(Marcus Börger)

22 years ago
# FROM: cshmoove@bellsouth.net # He couldn't mail to the list himself. Marcus, sorry for not posting to the NG but my email client at work (Lookout Express) is acting funny. feel free to post this to the NG as a point of reference. the one question/concern i have with the patch is consistency with the new read_dimension/write_dimension object handlers. seems like it would have a bit of the WTF factor on the end user side if the read_dimension handler and get_properties handlers return different results. in other words, s/he can use array_* functions on the object, but normal indexing may not work as expected. as an example, i have a UnicodeString class which uses the dimension handlers to return a Unicode codepoint at a given offset in the string. this takes only numeric indices. in addition i have a "length" property which is returned by the get_properties handler. do you see a problem in this scenario ? or do we request extension writers to support something like $myUnicodeString["length"] in the read_dimension handler as a matter of consistency (i would hope not)? or maybe we've come to the logical conclusion of trying to treat an object as an array in the first place. l0t3k Friday, September 19, 2003, 1:18:53 PM, you wrote:
> Hello internals,
> IIRC it was intended to use object properties as arrays. Currently most > array_*() functions do explicitly check for IS_ARRAY. The patch below now > enables objects in nearly all array_*() functions. The contray of such an > approach is that it would allow to access/modify protected and private > properties. But for some functions we do it already so we need to find a > solution for that sooner or later independantly from this patch.
> Any thoughts?
> http://marcus-boerger.de/php/ext/standard/php-array-20030918.diff.txt
> -- > Best regards, > Marcus mailto:mail@marcus-boerger.de
-- Best regards, Marcus mailto:helly@php.net

Marcus Börger

22 years ago
Hello, Friday, September 19, 2003, 9:08:10 PM, you wrote:
> [...] the one question/concern i have with the patch is consistency > with the new read_dimension/write_dimension object handlers. seems like > it would have a bit of the WTF factor on the end user side if the > read_dimension handler and get_properties handlers return different > results. in other words, s/he can use array_* functions on the object, but > normal indexing may not work as expected.
Well if you don't want array_*() function support for your objects then you can simply return NULL in get_properties() handler and voila.
> as an example, i have a UnicodeString class which uses the dimension > handlers to return a Unicode codepoint at a given offset in the string. > this takes only numeric indices. in addition i have a "length" property > which is returned by the get_properties handler.
> do you see a problem in this scenario ? or do we request extension writers > to support something like $myUnicodeString["length"] in the read_dimension > handler as a matter of consistency (i would hope not)?
And if you want to have both array behaviour and properties that are not handled by array_*() functions then you can simply handle them in the read_dimension/write_dimension() handlers and store them aside from the standard properties hash table. On the contray my patch allows objects to wrap around arrays and behave like arrays.
> Friday, September 19, 2003, 1:18:53 PM, you wrote:
>> Hello internals,
>> IIRC it was intended to use object properties as arrays. Currently most >> array_*() functions do explicitly check for IS_ARRAY. The patch below now >> enables objects in nearly all array_*() functions. The contray of such an >> approach is that it would allow to access/modify protected and private >> properties. But for some functions we do it already so we need to find a >> solution for that sooner or later independantly from this patch.
>> Any thoughts?
>> http://marcus-boerger.de/php/ext/standard/php-array-20030918.diff.txt
>> -- >> Best regards, >> Marcus mailto:mail@marcus-boerger.de
-- Best regards, Marcus mailto:helly@php.net

Zeev Suraski

22 years ago
I generally think that it isn't consistent with the clear separation that we're having in ZE2 - objects are no longer 'pretty arrays'. I think people should get used to it. Zeev At 14:18 19/09/2003, Marcus Börger wrote:

Andi Gutmans

22 years ago
I agree. The idea was to nuke this old "hackish behavior" with PHP 5. Arrays and Objects are two different beasts now. Andi At 10:34 PM 9/19/2003 +0300, Zeev Suraski wrote: