SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 36
  1. #1
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array wrapper class for PHP5

    Does anyone know of a wrapper class for arrays written in PHP5?

    You know, like a simple class where you could state something like
    PHP Code:
    $myArray = new Array() 
    and you would have an object with all the array functions as instance methods. The class would of course implement AccessArray, which would give you the best of both worlds, as you could access elements as both $myArray->element or $myArray['element'].

    I have been thinking of writing such a class myself, but it's quite a lot of work (although it can be done).

  2. #2
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe you're looking for ArrayObject.

  3. #3
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dbevfat
    Maybe you're looking for ArrayObject.
    Not really, although it might be a good start.

    ArrayObject only has a dozen or so methods; i'd like to see classic array functions turned to methods, so you could do things like:

    PHP Code:
    $arrayObject->sort();
    $keys $arrayObject->keys();
    $arrayObject->merge($secondArray); 
    Off Topic:

    Lep pozdrav, susjede!

  4. #4
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see. Maybe you could use this object (or even a brand new one) along with __call() and remap all methods to array functions? Maybe worth considering, but I think you should really start only with what you need and not overdo it from the beginning.

    OT: zdravo i tebi

  5. #5
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dbevfat
    I see. Maybe you could use this object (or even a brand new one) along with __call() and remap all methods to array functions? Maybe worth considering, but I think you should really start only with what you need and not overdo it from the beginning.
    I was thinking along these lines, exactly. Of course the exact method calls should be changed a little as in most cases what is the first agument of a function would become the object itself, but that's the idea.

  6. #6
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm wondering why exactly do you need this? I can't help myself, but the case of "$name = new String()" pops to mind. You know, when people try to complicate and mimic Java, when using plain old string is ... just fine? Your case isn't all like that, but it's similar from that perspective.

    I'm still interested on the results, so please post when (if) you make it work.

  7. #7
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd seriously love for native object types like String and Array to be introduced.

  8. #8
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is some use in it, but I feel like we can go without.

  9. #9
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Pittsburgh, Los Angeles
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You know, when people try to complicate and mimic Java
    Yeah...I at times wonder why they just don't use Java. Or some other language that has better OO support (or should I say a platform that is consistently OO).

    Also this:
    PHP Code:
    $arrayObject->sort(); 
    Would have to be something like:
    PHP Code:
    $arrayObject->sort($comparator
    The array isn't going to know how to order matters during the sort.

  10. #10
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The reason i'd like to have an Array object is not at all to emulate Java, but to put all the array functions (there is a bunch of them and they are all useful) into an more intuitive format. So I can write $arrayObject->contains($value) rather than in_array($value, $array).

    Quote Originally Posted by Snaily
    Yeah...I at times wonder why they just don't use Java. Or some other language that has better OO support (or should I say a platform that is consistently OO).
    I agree with you, and nobody would be happier than me if I could just use Javascript for my server-side development. (Except for JScript, of course.)

  11. #11
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, this is actually quite funny. I'm working on such a class right now. I thought about using ArrayObject, but as you pointed out, that doesn't include many methods. I'm also working on creating a few common data structures that don't have native support in PHP.

    As for the comment of emulating Java, I agree that sometimes people go a little too far with copying Java, but it doesn't mean we should all switch to Java. I can think of a lot of reasons I prefer programming in PHP. I have tried Java before but soon gave it up.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  12. #12
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Pittsburgh, Los Angeles
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but it doesn't mean we should all switch to Java.
    Not necessarily Java. If you are serious about OO sticking with PHP makes no sense at all. There are other languages/platforms that do OO so much better. Of couse if your customers require or your job requires PHP there isn't much you can do but make the best of matters.

    Anyhow, having an array object would be nice in PHP. Particularly to get more consistent naming of operations etc, but I would worry about performance. But I'm anal about such thigns.

  13. #13
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know. Care to give a few names of languages you are thinking of? i've been around the block with several languages and consistently end up coming back to PHP.

    Wouldn't you say PHP5 is a lot better with object-orientation, though? Given, I wouldn't touch PHP4 anymore because of its lack of decent support for OOP.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  14. #14
    SitePoint Guru
    Join Date
    Feb 2006
    Location
    Pittsburgh, Los Angeles
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't you say PHP5 is a lot better with object-orientation, though?
    Sure its much better. The real problem is that things aren't consistent, under the hood PHP is still procedural and it shows. So often to deal with this people do things like wrap built in functionality (such as the topic of this thread). This costs both in time and performance. Whereas other languages like Java, C#, Ruby and Python were built around OOP. So if you have the option why use PHP? To me it makes no sense.

  15. #15
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can just extend ArrayObject and add the methods you need.

  16. #16
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Snaily
    Not necessarily Java. If you are serious about OO sticking with PHP makes no sense at all. There are other languages/platforms that do OO so much better. Of couse if your customers require or your job requires PHP there isn't much you can do but make the best of matters.
    I don't think there is any such thing as a perfect language. PHP isn't very pretty, but it has other qualities, the biggest of which is it's community and the sheer number of proficient developers.
    I think an OO array wrapper is a mistake in PHP btw. PHP's native array support may be procedural, but it's very powerful. I prefer to go the other way around and use SPL to let objects behave like primitives.

  17. #17
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > I think an OO array wrapper is a mistake in PHP btw. PHP's native array support may be
    > procedural, but it's very powerful.



    What is needed more than anything, is a Date Range implementation, which I'll have the need of said implementation very soon; Looks like I'll be left to do the implementation myself...

    But it should be part of the core in my view.

  18. #18
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    PHP's native array support may be procedural, but it's very powerful.
    It also confounds arrays and hashes, which can be annoying in some cases...

  19. #19
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree the procedural arrays are powerful, but it'd be great to have all of those functions organized into an array class.

    Off Topic:

    Quote Originally Posted by Dr Livingston
    What is needed more than anything, is a Date Range implementation, which I'll have the need of said implementation very soon; Looks like I'll be left to do the implementation myself...

    But it should be part of the core in my view.
    Ack, I wish more than anything we could have overloaded operators, so we could do something like:

    PHP Code:
    $date_difference $date2 $date1
    Where $date1 and $date2 were instances of some DateTime class and the result would be a TimeSpan instance.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  20. #20
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by devbanana
    Ack, I wish more than anything we could have overloaded operators
    http://pecl.php.net/package/operator

    Only a PECL package at the moment, but you never know, it or something similar could make it into the PHP 6 core

  21. #21
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devbanana
    Ack, I wish more than anything we could have overloaded operators, so we could do something like:
    It makes me a bit fearful as to the amount of clutter they could potentially introduce into an already very feature-rich language.

    Quote Originally Posted by devbanana
    PHP Code:
    $date_difference $date2 $date1
    How about :
    PHP Code:
    $date_difference $date2->subtract($date1); 

  22. #22
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kyb: good point. A while ago I was trying to make up an example where operator overloading would be superior. I couldn't come up with any!

    All I could think was stuff like:
    $date = $date2 - $date1;

    and:
    $cart += $item + $item + $item;

    All this doesn't add much to language. The latter example is actually and example on why not to user the overloading - this code can be confusing like no other can.

  23. #23
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    PHP Code:
    $date_difference $date2->subtract($date1); 
    What's the difference - except that the other one is shorter by a great deal?

  24. #24
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know you can use methods like subtract, add, etc, but it's a lot shorter and still intuitive, when used properly, if you could use operators like - or +. I personally would love if it'd make it into the PHP core.

    By the way, dreamscape, that link seems not to work. Is it the correct link? I'd like to check out that extension just for reference.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  25. #25
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devbanana
    By the way, dreamscape, that link seems not to work. Is it the correct link? I'd like to check out that extension just for reference.
    It works fine for me. Perhaps PECL is down from some locations? It was acting up for me earlier. Anyways, you can also find it by going to PECL and searching for "operator". It's the only extension that comes up in the results.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •