Take the tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm studying OOP PHP and have watched two tutorials that implement user login\registration system as an example. But implementation varies. Which way will be more correct one to work with data such as this?

  1. Load all data retrieved from database as array into a property called something like _data on class creation and further methods operate with this property

  2. Create separate properties for each field retrieved from database, on class creation load all data fields into respective properties and operate with that properties separately?

Then let's say I want to create a method that returns a list of all users with their data. Which way is better?

  1. Method that returns just an array of userdata like this:

    Array([0]=>array([id] => 1, [username] => 'John', ...), 
          [1]=>array([id] => 2, [username] => 'Jack', ...), 
          ...)
    
  2. Method that creates a new instance of it's class for each user and returns an array of objects

share|improve this question
1  
In short. 2 is better. –  Ozair Kafray Nov 21 at 6:22
 
I agree and recommend to have a look at ORMs like Doctrine or Propel. And try to avoid arrays whenever possible since they are kind of anti-OOP (e.g. no type hinting)... see the php manual for alternatives like Traversable, Iterator and ArrayAccess. –  AaL Nov 21 at 10:00
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.