I find that even when working with a MVC framework (e.g. CodeIgniter), I regularly resort to passing nested arrays rather than objects.

This doesn't follow my understanding of object-oriented well. For example, if I were to port the code to Java then I wouldn't dream of passing arrays (or Vectors, or whatever) around like that.

I was wondering if this is the appropriate way to handle data. Is there a reason why arrays are passed around like this in PHP, or why objects aren't used? What's the best way to pass data around?

4 Answers 4


Don't confuse Java's OO with PHP's. Java is a single paradigm language which means that it only does OO. PHP on the other hand is a multi paradigm language, you can either do functional programming or OO or both.

Now there is no such thing as a "bad" implementation of OO. Java's OO is not a definitive implementation that every other language should or must follow. There are some definitive concepts, and both languages fully implement them in their own way (Java since the beginning, PHP since version 5).

So, to answer your question: What CI does and what you are doing with it is right in the PHP world. PHP's arrays are one of its most flexible and useful structures and it's actually a good thing to use arrays over objects when your data are just information (do not carry logic with them). Fully OO code is not the same thing as "only OO code".

If you are starting with PHP do use Java as a reference for good OO practices but do not limit your understanding of PHP because of the "Java does it differently" thing. You can really screw up in both, the paradigm will not save you if you don't get the concepts.

If in any case you find Java more to your liking (as a personal preference) do check out Ruby for web development, they are a little bit more closely related.

  • Why should we not confuse Java OO with PHP OO? They are pretty much the same, except that PHP has a native array/hash datatype. May 28, 2011 at 13:06
  • The implementations are quite similar. I was referring to how they are used in both languages.
    – yannis
    May 28, 2011 at 16:39
  • I have an issue in OOP. I want to create OOP design for an application (PHP base), where one object will depends on other objects. How do i handle this in PHP? plz help me...
    – Imran Khan
    Nov 26, 2014 at 13:49
  • Example Scenario: Hotel object have Room object's, where a room object have to Date objects... and a Date object have prices for person types. Now how to handle this type of scenario in PHP by OOP base (because there are large data which needs processing at each level).
    – Imran Khan
    Nov 26, 2014 at 13:52
  • @Walter i'm waiting for your response.... plz help me.
    – Imran Khan
    Nov 28, 2014 at 11:43

Usings objects instead of arrays just because it's objects is not OO paradigm, it's just personal preferences :)

Objects gives handy code completion in IDE, interfaces (type hinting) and inheritance can be used.

If you want to use objects instead of array because you see any benefits - use them, but if you want to use them just because it's objects - don't waste your time to this refactoring :)

  • "usage objects just as arrays will take more memory and CPU." This is not (always) true. An object holding the same amount of data with an array will occupy almost exactly the same amount of memory.
    – yannis
    May 27, 2011 at 21:26
  • @Yannis Rizos, yes, objects even can use less memory, edited.
    – OZ_
    May 27, 2011 at 21:37
  • It's not just that they can use less memory, it's that their memory use is irrelevant because it's trivial compared to other memory performance aspects of the system and to the benefits of using objects. If we cared about memory usage so much, we would never use objects. What we care about is that objects give us better tools to reason about and construct systems. May 27, 2011 at 21:44
  • @Rein Henrichs, I know it, thanks. That's why all words about 'memory' was removed from my answer. I'm not trying to do silly optimization, but I'm sure objects just because they are objects is not a reason for refactoring :) Read my answer, not only comments.
    – OZ_
    May 27, 2011 at 21:49
  • fair enough, I commented before the edits. May 27, 2011 at 21:50

You don't really pass data around in an OO system -- you pass objects around. The difference is that objects contains behavior as well as data. That is why they call it object oriented and not data oriented.

As long as your don't need behavior with your data, then plain old php arrays are just as good (or bad, depending on your point of view) as value objects.


I think this is just a question of adjustment - there are many implementations of "objects" in programming - Python and Javascript have strikingly different properties. PHP OO is arguably a hack - PHP arrays aren't "objects" in the traditional sense - but they serve an obvious purpose. Unless you want the data to have custom BEHAVIOUR then why use an object?


re: immutable value objects


  • To give the data a reliable structure. Yep, I can build a function that outputs a particular format/nesting of array but who's to say I don't write another function that modifies it slightly and then passes it on.
    – Matt
    May 28, 2011 at 0:03
  • 1
    sure - then you would need an object to protect variable access - since there are no PHP equivalents of Python tuples for example. But there you go - you've defined the problem, you've just got to use the constructs available to the language... or just learn to live with it's "quirks". I've added a link to my initial post that has an interesting discussion of the problem.
    – sunwukung
    May 28, 2011 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.