September 12, 2013

The OOP way of doing things (in PHP)

Marcos’s Question:

I’m still trying to wrap my head around OOP, having been brought up in procedural coding a long time ago. Been stretching my legs in PHP for a while now.

So I’ve read a lot about best practices and paradigms and such. Especially SOLID. And I’ve tried to do things in a number of different ways. MVC-style, I ended up with extremely long, convoluted models that were either tightly couple to everything else, or had to be injected with half a dozen dependencies. Then I swung back to fat controllers, who did pretty much all the work and the models were only like helpers.

Now, I need a judgement on my current approach. Would this be a valid (SOLID) OOP way of doing a user-account-management system:

class PDO_Wrapper { # wraps PHP's PDO functions nicely
    public function select(...);
    public function insert(...);
    public function delete(...);
    public function update(...);
}

class ORM extends PDO_Wrapper { # very basic
    public function load($id); # assigns everything in row $id to object paramters
    public function save(); # writes all object parameters to db
}

class User extends ORM { # some User specific functions
    public function hashPassword($password);
    public function sendConfirmationMail();
    ...
}

class Login extends User { # one model for each User action (login, register, forgot password...)
    public function login($input);
}

Well, I dont know which one of SOLID priniciples are you following:

Lets break down your code

class ORM extends PDO_Wrapper { # very basic
    public function load($id); # assigns everything in row $id to object paramters
    public function save(); # writes all object parameters to db
}

This class, is extending PDO_Wrapper. Now lets ask, does it provide an extension of PDO_Wrapper? Actually It does, so its kind of correct. But for this:

class User extends ORM { # some User specific functions
    public function hashPassword($password);
    public function sendConfirmationMail();
    ...
}

Does User extend the ORM in some way? NO, it uses it! So very wrong. You have to create an object of it inside the class and use it like:

class User {
   private $orm_db;

   function __construct() {
        $this -> orm_db = new ORM();
   }
}

Now for this:

class Login extends User { # one model for each User action (login, register, forgot password...)
    public function login($input);
}

Login is an action done by the object of User class. So it is a method of User Class, not a sub class.

class User {
private $orm_db;

   function __construct() {
        $this -> orm_db = new ORM();
   }

   public function login($input) {
        //...
   }
}

I hope it clears some concept about OOP.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!