1

I have a question on best practices for PHP OOP, relating the using global variables in methods. I know what I am doing is incorrect, but I am not certain how else it should be done.

We are using this database class: https://github.com/joshcam/PHP-MySQLi-Database-Class/blob/master/MysqliDb.php

We create the object in a config.php file that is required on every page with the following line of code. $db = new MysqliDb(host,user,pwd,dbname);

However, every method in all of our classes must use global $db if we wish to access the database. Is this bad practice? If so, how should this be done? Should all of our classes be extending the database class (MysqlDb)?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Bobby Bruce
  • 341
  • 5
  • 12
  • I am not sure if it applies to your case but it is possible to pass the $db object as an argument to the constructor/function of another class via PHP Type Hinting: http://stackoverflow.com/questions/5724677/php-type-hinting-to-primitive-values. Using this, you can declare $db as a private variable/object of those other classes and initiate it via constructor of those classes. Then all the functions of those classes will have access to the $db object without having to declare it as a global variable in each function/method. – Maximus2012 May 06 '15 at 14:41
  • 4
    Inject $db into the constructor for all your classes, and set it as a property of that class so that it's available to all methods (Dependency Injection) – Mark Baker May 06 '15 at 14:42
  • @MarkBaker do you think my comment pointed to the same thing as what you suggested? I am not very clear on the concept of Dependency Injection myself. Are we talking about the same thing ? – Maximus2012 May 06 '15 at 14:44
  • Why don't you use any registry patterns? – B001ᛦ May 06 '15 at 14:44
  • That makes excellent sense (adding it to the constructor). Would that still be done using `global $db`? This may be slightly off topic and is for my own educated, but what will be affected if the variable was marked as public instead of private? Would this cause it to conflict with other classes... ? – Bobby Bruce May 06 '15 at 14:47
  • While Dependency Injection seems the way to go, you might also have a $db property in every class, and your constructor might set $this -> db = new Db(); You could write a "mother" class where all your classes are derived from that actually handles that for you. That way you would still be in line with the single responsibility principle (i think), save a lot of writing / error prone-ness, and have your DB class in the object scope. – Burki May 06 '15 at 14:47
  • I'm not certain what registry patterns are, but I am going to Google that. – Bobby Bruce May 06 '15 at 14:47
  • The one caveat I see is that the constructor is not called on static methods. Any work around for this? – Bobby Bruce May 06 '15 at 14:50

1 Answers1

0

mysqli-db has a MysqliDb::getInstance() static call to get an initialized object. So you initialize it in config.php with new() and after that use MysqliDb::getInstance() to get an object.

dakab
  • 5,379
  • 9
  • 43
  • 67
alex b
  • 24
  • 3