Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p><em>When should I use 'extends' rather than passing $db to the class, assuming every class uses the db?</em></p> </blockquote> <p>When it makes sense -- and only when it does !</p> <p>You have at least two things to consider :</p> <ul> <li>"<code>class A extends B</code>" kind of means "<code>class A **is a** B</code>" <ul> <li>more clearly, a <code>Car</code> is a <code>MotorVehicule</code> ; a <code>MotorVehicule</code> is a <code>Vehicule</code> ; a <code>Bus</code> is a <code>MotorVehicule</code> ; a Bicycle is a <code>Vehicule</code></li> <li>however, a <code>Ball</code> is not a <code>Vehicule</code></li> <li>In your case, a <code>Form</code> is definitly not a <code>DataBase</code> ! Nor is a <code>Login</code></li> </ul></li> <li>In PHP, a class can only <code>extend</code> <strong>one</strong> class <ul> <li>You can not have something being both a <code>Vehicule</code> and an <code>Animal</code></li> <li>But a <code>Car</code> <strong>is a</strong> <code>MotorVehicule</code>, which, itself, <strong>is a</strong> <code>Vehicule</code> <strong>:-)</strong></li> </ul></li> </ul> <p>In the case of a Database object <em>(in your case, it's more a connection to a DB)</em>, mosts of your classes will not themselves "be" a database connection. So, they shouldn't extend that class.</p> <p>However, they are using a DB connection <em>(a <code>Form</code> "<strong>has a</strong>" DB connection)</em> ; so, they should have a property representing that DB connection. That's what you are doing.</p> <p><br> Instead of passing <code>$db</code> to each constructor, you might use</p> <ul> <li>either the <a href="http://en.wikipedia.org/wiki/Singleton" rel="noreferrer">Singleton</a> design pattern</li> <li>or the Registry design pattern</li> <li>or some kind of global variable, but that's almost the same... just being worse <em>(not OOP and all that)</em> !</li> </ul> <p>But passing the <code>$db</code> object is great for unit-testing, mock objects, and all that... <br>I think it could be considered as being the Dependancy Injection design pattern, btw <em>(not sure, but looks like it)</em></p> <hr> <p>About loading lots of classes, other people gave answers :</p> <ul> <li>Use autoloading if you can</li> <li>Use an opcode cache, like APC, if you can</li> </ul> <p>Both of those are great suggestions that you should take into consideration <strong>;-)</strong></p> <hr> <p>One last thing :</p> <blockquote> <p><em>Does passing $db to every class hurt in any way in terms of performance?</em></p> </blockquote> <p>Maybe it does a <strong>little</strong> bit ; but, honnestly, except if you are google and have millions of users... who cares ?</p> <p>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 ! <br>So, the small amount of time used passing paremeters can probably be neglected <strong>:-)</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload