Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to structure classes in PHP

I have been working on moving over to OOP in PHP. I have reading explanations on php.net, but I was hoping I could get some specific answers here.

I tried to create the following example to illustrate my question. Say I have "Database", "Products", and "Users" classes, and I want to display products if a user has access.

So I call the "Products" class "showProducts()" function, which in turn creates an instance of the "User" class, which creates an instance of the "Database" object and checks the users access level.

If the user has access, then the "showProducts()" function creates another instance of the "Database" object, and queries the database.

class Database{

   public function query(){ 
      //runs query here 
   }

   public function __construct() { 
      //sets up connection here 
   }

}

class User{

   public function checkAccess(){ 
      $db = new Database(); 
      $db->query( //pass in query to check access )
      //does stuff, then returns true or false
   }

}

class Products{

   public function showProducts(){

      $user = new User();

      if($user->checkAccess())
         $db = new Database(); 
         $db->query( //pass in query to get products )
      }

}

I was hoping someone could illustrate how to do this the proper way.

I would like to have some sort of controller class, that creates one "Database" object, that is available to all of the classes that need to access it, without having to create multiple instances of the "Database" object. I would like the same thing with the users class, so there is one $users object that all the classes can access, without having to create a new object every time I need to use something in the "User" class.

I apologize if my question is not clear, and thanks in advance for any responses!!

Thanks to everybody for the replies!

like image 662
Ben Avatar asked Oct 09 '12 19:10

Ben


1 Answers

When moving form procedural to Object Oriented programming you should grasp more then just how to build classes. OOP is not writing classes, its about following best practices, principles and patterns in OOP.

You should not instantiate new objects inside another, you should give the User object, his Database object that User depends on, through constructor, or setter method. That is called Dependency Injection. The goal is to give objects to a class that needs them through constructor or setter method. And they should be instanciated from outside of that class, so its easier to configure class. And when building a class you want its easy to see what dependencies that class have. You can read about Inversion of Control principle here: IoC

So then your code would look like this:

<?php

// User object that depends on Database object, and expects it in constructor.

class User
{
    protected $database;

    public function __construct($database)
    {
        $this->database = $database;
    }

    // -- SNIP --
}

?>

Now to use that user class you do this:

<?php

    $database = new Database($connParams);
    $user = new User($database);

?>

You can also use Dependency Injection using setter methods to set dependencies, but Il let you google that for yourself :)

Thats it, joust read about Inversion of Controll principle, and about Dependency Injection and Dependency Injection Containers, these are the best ways to manage classes.

I have seen lots of PHP code that is "OOP" and in fact they are only using Classes as functionality namespaces :) So joust learn about OOP principles and patterns.

Have fun! :)

like image 152
otporan Avatar answered Sep 22 '22 14:09

otporan