Jeremy Cook’s Blog: Implementing the ArrayAccess Interface

Jeremy Cook is back with the next part of his series looking at the handy features PHP’s SPL provides. In this new post he looks at the ArrayAccess interface and how it can make your data more accessible to PHP’s own array handing functions.

ArrayAccess allows you to treat an object that implements it as if it is an array for the purposes of setting, unsetting and retrieving data from it. Please note the emphasis in the last sentence! ArrayAccess does not make an object behave like an array in any other way. If you pass an object that implements ArrayAccess to a PHP array function such as in_array() you’ll still get an error. This will become a little clearer with some of the examples below.

He shows what you’ll need to use this interface in your class – implementing the interface and defining a set of four methods to get/set and check for the value in your array. He includes a practical example of pulling data back from an API and wrapping it in a class to make accessing it simpler (also implementing the Countable interface as well, see the previous post for more on that). Code is include to illustrate how it can be used.

Both comments and pings are currently closed.

18 Responses to “Jeremy Cook’s Blog: Implementing the ArrayAccess Interface”

  1. new blog guide…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  2. boredom guru says:

    boredom guru…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  3. garden fencing…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  4. mini bus hire hertfordshire…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  5. data recovery from hard drive…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  6. best lawn tractor…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  7. Regina Website Design…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  8. 2000 mustang says:

    2000 mustang…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  9. Engraved Pewter Tankard…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  10. twitter marketing…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  11. asian dating…

    I was recommended this website by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem. You are amazing! Thanks!…

  12. frederick computer…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  13. lawn mulching…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  14. dating free sites…

    Interesting post here. One thing I would like to say is the fact most professional areas consider the Bachelors Degree just as the entry level standard for an online degree. Though Associate Degrees are a great way to get started, completing a person’…

  15. Bebo says:

    Bebo…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  16. kindle fire says:

    kindle fire…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  17. Sondage Rémunéré…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…

  18. watch the dark knight rises online…

    [...]Jeremy Cook’s Blog: Implementing the ArrayAccess Interface | PHP Blog[...]…