5

In the framework I am using, I shall group things into models, controller, and views (I added services).

When getting things out from database, it is pure data in array form. So they won't have methods with them.

As the title asked, is it common sense to convert database data to object first? That is to say, I may then have five layers in my project, model, service, object, controller, view.

And I am very puzzled on how to sort these layers or how to specify their jobs. And it also concerns me that if there be any multi-thread problems, IO problems and such.


I have loads of questions and this is only one of them.

I used to use JavaSE to make some game bot programs and games. OOP seems quite nice and I love it very much. I store everything as object in the RAM or sometimes serialized in txt file.

But now I am doing a PHP job building website. When I met MVC and database, things changed that I created much fewer object and tackle with more arrays. Sometimes I need to do something with the data like isValid(), I have to create a function somewhere - where I am not sure to put - and takes in a param like isValid($obj).

4
  • The Object in your five layers is your Model. Model could be whatever you need it to be, i.e. if you only need to iterate then array should be fine.
    – imel96
    Dec 21, 2016 at 5:49
  • 1
    There's no such thing as common sense. Dec 21, 2016 at 5:52
  • 2
    You can certainly just use arrays if you like. That is, it's going to work... On the other hand most people prefer to have an actual object with properties instead of having to refer to rows[row_num][5] in order to print out that product title.
    – Sklivvz
    Dec 21, 2016 at 10:40
  • 1
    @Sklivvz - php arrays are also maps so $row['title'] will usually work as well.
    – Cerad
    Dec 22, 2016 at 3:35

1 Answer 1

6

Honestly, I would add a row mapper or an ORM to the project.

Even for developers, to read data in such format (arrays) is tedious and complicated. After a couple hours, the readability becomes blurred and cryptic. It's exhausting.

As developers, (excuse me in advance if you disagree) is easier for us to think in data structures as objects, attributes and properties.

As you work, you will find to be easier the implementation of patterns or design methodologies using objects rather than arrays. Think in these wonderful words: composition, inheritance, aggregation, ...

From the code point of view, working with arrays makes the code more difficult to maintain. The order of the fields into the array is imposed by the SQL statement. It means that, if you add, remove or change the select statement, you have to review how the code access to the array (all over the project).

Working with arrays, you also miss many of the IDE assistances, hints and helps.

One idea beneath the OOP is modelling things like in the real word using words of the real world too. Unless you live in Matrix, to think in terms of arrays and indexes is unnatural and for most of the cases, unnecessary.

But, that's my sense.

Nevertheless, I have to agree with @whatisaname

There's not such thing as common sense

So, do whatever makes your job easier

2
  • Thank you for your patient answer :) Any Further readings on how to add a row-mapper or a ORM?
    – bijiDango
    Dec 22, 2016 at 5:43
  • That depends on the project. Ask first to your company if you can add one. Some times is matter of budget and time.
    – Laiv
    Dec 22, 2016 at 8:55

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.