How to structure classes in PHP

I am working on the transition to OOP in PHP. I read the 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. Let's say I have the classes "Database", "Products" and "Users", and I want to display products if the user has access.

So, I call the showProducts () function "Products", which, in turn, creates an instance of the "User" class, which creates an instance of the "Database" object and checks the user 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 would be able to illustrate how to do this properly.

I would like to have some kind of controller class that creates a single "Database" object, accessible to all classes that must access it, without having to create multiple instances of the "Database" object. I would like the same with the user class, so there is one $ users object that all 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 answers !!

Thanks everyone for the answers!

+7
source share
3 answers

When moving a formal procedure to object-oriented programming, you need to understand more about how to create classes. OOP does not write classes about how to follow recommendations, principles, and patterns in OOP.

You should not create new objects inside another, you must specify the User object, its database object on which the User depends, using the constructor or the setter method. This is called dependency injection. The goal is to provide objects to the class they need using the constructor or setter method. And they must be implemented outside of this class, so itโ€™s easier to set up the class. And when creating a class, you want it to be easy to see which dependencies this class has. Here you can read about the principle of control inversion: IoC

So your code will 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 this user class, you do this:

 <?php $database = new Database($connParams); $user = new User($database); ?> 

You can also use Injection Dependency with setter methods to set dependencies, but Il allows you to do this yourself :)

This is what I read about the principles of inversion of the Controll principle, as well as about containers for injecting dependencies and dependencies, these are the best ways to manage classes.

I saw a lot of PHP code, which is "OOP", and in fact they only use class classes as namespaces of features :) So, find out about the principles and patterns of OOP.

Good luck! :)

+11
source

Do not create objects inside your constructors or other methods. Pass them as a parameter, preferably inside another class known as factory. This will simplify the verification of your code, as well as simplify the creation of objects.

Also, do not try to use singletones. This is an object-oriented version of "global variables" and you do not want to use global variables. This makes testing your code very difficult, almost impossible.

Watch this video http://www.youtube.com/watch?v=-FRm3VPhseI to understand why singles are bad to use. Of particular note is the CreditCard example at 19:00.

If you really want to do this in a modern way, take a look at the concept of dependency injection. In essence, passing material that is required externally to the class is a complete secret, but there are frameworks that do this automatically for you, so you no longer need to write a factory. They are called a Dependency Injection Container or DIC.

+2
source

To make a single object for all of your code, use the Singleton pattern:

 class Database{ private $db_descriptor; private function __construct(){ /* connect and other stuff */ } public static function getInstance(){ static $instance; if($instance === null){ $instance = new self(); } return $instance; } } 

And you can use the same technique with users, I say more with php 5.4, you can use 1 trait for singleton pattern. Last tip: when you are working with a database and other hard things, use the lazy initialization method. When you improve your OOP skills, see the Doctrine Project , they use these methods a lot!

0
source

All Articles