Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is passing data in arrays not discouraged in CodeIgniter?

I come from a Java background and have only recently started learning PHP and CodeIgniter.

While I find the framework awesome for its clean design and impressive documentation, I notice that the framework doesn't necessarily discourage the use of data arrays instead of value objects for passing data around. For ex., the database queries return the result in an array which you can then pass over to the views for rendering. Similarly, most of the core library methods take associative arrays as inputs.

This, to me, seems like a bad design for an Object Oriented language which should promote and maybe even enforce using value objects for their obvious benefits of encapsulation.

Is this really an example of bad design or simply a matter of style/preference ? Are there any obvious benefits of using arrays for data over a more OO approach ?

like image 825
Rahul Avatar asked Feb 28 '26 04:02

Rahul


2 Answers

Don't be misled by the myth that "everything has to be an object" for your code to be "good Object Oriented design". When you start trying to formulate rationalisations that "I shouldn't be allowed to do this, because it's not good OOP", you're programming backwards.

When you want a list of pieces of data, a data array will suffice. Indeed, it's appropriate.

like image 182
Lightness Races in Orbit Avatar answered Mar 01 '26 16:03

Lightness Races in Orbit


Why build a complicated object structure when a hash will do? IMHO, many things in the java world are over-engineered. This opinion seems to be shared with many dynamic languages and toolsets, such as Ruby on Rails.

like image 43
DGM Avatar answered Mar 01 '26 16:03

DGM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!