0

The other day, while developing my PHP project and implementing the User class, i started to wonder how this class should interact with the MySQL database the project is using.

Let me start with an example: let's say I have a getName() method, inside the User class, that returns the user's real name. What's the more fitting way to implement that method?

I came up with 2 solutions:

  1. I put the DB query inside the getName() and only get what I need like this:

    public function getName() {
    
        // MySQL query code here
    }
    
  2. I create a load() method inside the User class that load all the user data inside the class structure and then the getName() is something like this:

    private $name;
    
    // Load all user data inside the class' structure
    public function load() {
    
        $this->name = // MySQL query here
    }
    
    public function getName() {
    
        return $this->name;
    }
    

I thought, not sure if mistakenly or not, that the first way is more efficient because i only get the data I need, while the second way is more OOP but less efficient.

So, here's the question: what is the better way? Why? Are there better ways?

3
  • 1
    Either way, consider storing/caching the results of that so you do not make a query every time you use getName on that object. Also, consider not wrrying about all that by using a ORM/DBAL Solution like propel or doctrine stackoverflow.com/questions/2062473/php-orms-doctrine-vs-propel Commented Apr 3, 2014 at 9:08
  • 1
    Also check out Lazy Loading and the Active Record Pattern en.wikipedia.org/wiki/Active_record_pattern Commented Apr 3, 2014 at 9:11
  • @AndreschSerj I found your comment very helpful, if you write down the answer I'll mark it :) Commented Apr 11, 2014 at 12:17

3 Answers 3

1

Either way, consider storing/caching the results of that so you do not make a query every time you use getName on that object. Also, consider not wrrying about all that by using a ORM/DBAL Solution like propel or doctrine.

Also check out Lazy Loading and the Active Record Pattern

Sign up to request clarification or add additional context in comments.

Comments

1

Run your query just in time and only run it once (unless you know the value might change), try something like the following:

 class User {
   protected $data;

   function getName()
   {
     if (!isset($data['name'])) {
       // if you can load more than just $this->data['name'] in one query
       // you probably should.
       $this->data['name'] = // query here
     }
     return $this->data['name'];
   }

 }

3 Comments

Don't you think the overhead of reading probably 6 fields in one query is worth not doing 6 queries and thus, the load should load all the cols from the table involved?
@AndreschSerj loading all columns would be best, instead of $this->data['name'], maybe just $this->data = getQueryResultsAsArra();
What i meant exactly :D
0

Aside from the question being kinda broad (as there are countless patterns), the second way you mentioned is better IMO, and to add to it I would also suggest supplying ID as a parameter which you could then use to build a single query to fetch the user by ID and then manually assign all properties (from the fetched row).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.