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)
.
1 Answer 1
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
-
Thank you for your patient answer :) Any Further readings on how to add a row-mapper or a ORM?bijiDango– bijiDango2016年12月22日 05:43:18 +00:00Commented 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– Laiv2016年12月22日 08:55:24 +00:00Commented Dec 22, 2016 at 8:55
rows[row_num][5]
in order to print out that product title.