KBD

Keith Devens .com

Monday, December 1, 2008 Flag waving
Less isn't more; just enough is more. – Milton Glaser
← Shouldn't you lose your job when you do something like this?The Taoist Parable About Luck →

Daily link icon Thursday, August 5, 2004

The feature I most want in PHP 5.1:

You can overload method calls and property accesses in PHP 5. But it doesn't seem there's a way to overload array indexing so you can make an object behave like an array or dictionary. I'd love to see that become possible in PHP 5.1.

However, realistically, it's probably a total mess if you want to do that thoroughly. By thoroughly I mean that an object can behave completely like an array and can have all the array functions applied to it, including being sortable, being able to make current, next, prev (etc.) calls on it, do set differences, unions, and intersections on it, do slicing, and on and on. PHP's array type is so overloaded that making an object behave like a built-in array is probably pretty hairy.

However, I'd still like to be able to hand something an object and have other code not care whether it's actually an array or not, at least for indexing into it. For instance, say some function takes an array now -- I'd like to be able to replace that with an array-like-object that's built lazily and implements an iterator.

Python does this -- it allows you to provide array-like access to an object. How does it handle all the cases? I want this in PHP! Smiley

Update: Here's how Python does it. That's not so bad. PHP would need a little more because arrays in PHP maintain a current index as well as remember the order of insertion.

← Shouldn't you lose your job when you do something like this?The Taoist Parable About Luck →

Comments XML gif

Ian Bicking (http://blog.ianbicking.org) wrote:

Python also has classes like UserDict.DictMixin which will allow you to implement a small number of methods, and all the other methods are filled in for you. All you have to set is methods for getting, setting, deleting, and listing all keys.

∴ Ian Bicking | 5-Aug-2004 12:22pm est | http://blog.ianbicking.org | #5182

Harry Fuecks (http://www.phppatterns.com) wrote:

Agreed. Would also be good to do the reverse. The new SPL extension http://www.php.net/spl allows you to define classes which implement special iterator interfaces. Once done you can iterate of objects of the class using foreach. Unfortunatly nothing else, array-wise, seems to work (such as using array_map() - would be nice if the interface defined something to allow that)

∴ Harry Fuecks | 5-Aug-2004 3:38pm est | http://www.phppatterns.com | #5185

Keith (http://keithdevens.com/) wrote:

Python also has classes like UserDict.DictMixin...

Ah! I was thinking that there should be something exactly like that! Thanks for the info.

Keith | 5-Aug-2004 4:38pm est | http://keithdevens.com/ | #5186

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

December 2008
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 6 posts

Recent comments XML

new⇒Perl 6 1.0 in March?

No, Parrot is not Perl 6.  Parrot​is a prerequisite for Rakudo Perl​1.0, bu...

Andy Lester: Dec 1, 8:40pm

new⇒Free image hosting sites

Well, TinyPic has this in its​FAQ:

> Images and videos is in​your accoun...

Keith: Dec 1, 1:13am

Join a NameValueCollection into a querystring in C#

Well with a lamba expression, this​is what I came up​with:

?!code:csharp...

Gustaf Lindqvist: Nov 30, 4:38pm

Why no generic OrderedDictionary?

Check​http://www.codeproject.com/KB/recip​es/GenericOrderedDictionary.aspx?d...

Gabrielk: Nov 27, 6:57am

WhatIsMyIP.com

http://www.thesysteminfo.com is​another good alternate to​whatismp.com... I...

Kripz: Nov 26, 8:51pm

Girls, please don't get breast implants

Actually I think it's sweet when a​man loves a woman whether she's big​or n...

218.186.12.228: Nov 26, 9:40am

Generated in about 0.206s.

(Used 8 db queries)

mobile phone