I have an includes.php page that I load at the start of every page of my website. As I develop the website, the number of classes that I am using is growing. So I end up with something like this:
$db = new DB($config);
$login = new Login($db, $config);
$form = new Form($db, $config);
And the list goes on and on. I have two questions about this practice:
First, Considering I might not be using a class at a certain page (I might not have a $form on every single page), how much does it really matter, performance-wise, to load this class every time any given page is loaded?
Second, you may have noticed that I am passing the class instance $db to all the other classes, as well as a variable $config. In the php code of every class, I do something like this:
public $db;
public $config;
public function __construct($db, $config, $smarty){
$this->db = $db;
$this->config = $config;
}
then in the class methods, I address the database and config files with 'this' as such:
public function myfunction(){
$this->db;
$this->config;
}
When should I use 'extends' rather than passing $db to the class, assuming every class uses the db? Does passing $db to every class hurt in any way in terms of performance?
Thanks!
When should I use 'extends' rather than passing $db to the class, assuming every class uses the db?
When it makes sense -- and only when it does !
You have at least two things to consider :
class A extends B
" kind of means "class A **is a** B
"
Car
is a MotorVehicule
; a MotorVehicule
is a Vehicule
; a Bus
is a MotorVehicule
; a Bicycle is a Vehicule
Ball
is not a Vehicule
Form
is definitly not a DataBase
! Nor is a Login
extend
one class
Vehicule
and an Animal
Car
is a MotorVehicule
, which, itself, is a Vehicule
:-)
In the case of a Database object (in your case, it's more a connection to a DB), mosts of your classes will not themselves "be" a database connection. So, they shouldn't extend that class.
However, they are using a DB connection (a Form
"has a" DB connection) ; so, they should have a property representing that DB connection. That's what you are doing.
Instead of passing $db
to each constructor, you might use
But passing the $db
object is great for unit-testing, mock objects, and all that...
I think it could be considered as being the Dependancy Injection design pattern, btw (not sure, but looks like it)
About loading lots of classes, other people gave answers :
Both of those are great suggestions that you should take into consideration ;-)
One last thing :
Does passing $db to every class hurt in any way in terms of performance?
Maybe it does a little bit ; but, honnestly, except if you are google and have millions of users... who cares ?
If you are doing a couple of DB queries, those will take LOTS of time, comparing to passing one more parameter to even a dozen methods !
So, the small amount of time used passing paremeters can probably be neglected :-)
Have you tried something like this?
function __autoload($class_name) {
require_once("includes/php/class." . $class_name . ".php");
}
So it only loads the class name when the class name is encountered. (Change the path to suit your php classes... mine are like class.Object.php, with the class name "Object").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With