CONVERT AN OBJECT TO ASSOCIATIVE ARRAY IN PHP

*
If you’re at home with languages like Perl or Javascript you may be used lớn treating objects & arrays as variations on a theme. PHP is traditionally stricter than that. You can create or change an element directly on an array with assignment, but khổng lồ set a new property on an object you generally need to create a setter method – or open up a property for access. With the ArrayAccess interface you can get the best of both worlds. It allows client code lớn access a PHP object like an array.

Bạn đang xem: Convert an object to associative array in php

In other words you can showroom and retrieve values as if you’re working with an associative array but with the encapsulated functionality of an object.

Note Whoah – jargon check. What did I mean by encapsulated functionality? In fact this is one of the coolest things about object-oriented coding – it simply means that any dirty logic, lượt thích the processing or checking of values, is hidden away on the inside behind a nice clean interface. There are two key benefits to lớn this. Firstly, when you place logic on the inside, your client code gets cleaner. It makes a simple call và any messy or complicated business happens elsewhere. Secondly, and probably more importantly, putting xúc tích in a class reduces duplication. You manage your complicated business in one place and, if you change it, you only need vị so once.

Xem thêm: Download Codeigniter 3 And 4, 10 Differences Between Codeigniter 3 And 4

In practical terms, accessing a PHP object like an array means that you have the convenience of an array but with the availability of additional processing of the data on the way in or out of the object. So how vì chưng we make this happen? It’s really only a matter of implementing a built in interface named ArrayAccess.

A thử nghiệm project

If you use Silex you may have come across Pimple, the cool but horribly-named dependency injection container which uses this technique to lớn great effect. In its honour I’m going to lớn create a much more basic class named Callbacky. Its job? to store callbacks as if they were values. These will be transparently invoked at access time. Here’s how you might use Callbacky:


$c = new Callbacky();$c<"sayhello"> = function($c) return "hello you!";;$c<"storemynumber"> = 5; print $c<"storemynumber">; // 5print $c<"sayhello">; // hello you!
So, even though the $c variable contains an object, I am able to lớn assign an integer value to it as if it’s an array. I can also assign an anonymous function to it. What’s more, when I access that element later, instead of getting the function back I get the result of having run it. Obviously, in a real usage, the callback would likely perform some more involved or expensive function. We might also allow for the configuration of callbacks – setting some khổng lồ be run only once, for example. (Interested in that? I added it on at the end as a bonus). Let’s press on with the simplest brief though.

Declaring the ArrayAccess class

That’s just a matter of implementing an interface: