I have developed a small MVC framework where I have a database library that is used through out the app for executing mysql queries, let me show u
a sample of it
// Library/Database.php
class Database
{
public static function Execute($sql)
{
// code here ...
mysql_query(...);
// some more code here ...
}
}
Now, I thought to create an ORM for my database and want my developers to use the ORM class instead of calling the Database class. The ORM class
goes something like this
// Library/ORM.php
class ORM extends Database
{
public function Select($col)
{
// code here ...
parent::Execute(...);
// some more code here ...
}
}
Now, How do I force my developers programatically to use the ORM class instead of using the Database class where ever a database interaction is
required?
Thanks in advance
PS: If my question is still not clear I shall be happy to explain it once again
error_reporting(E_ALL);
abstract class Database
{
public static function Execute($sql)
{
// code here ...
echo $sql . "\
";
// some more code here ...
}
}
Database::Execute('SELECT this FROM that');
Change the statics into regular methods
Fire anyone who calls the Database method directly.
If you don’t want the Database methods called directly, you’ll have to make them protected, but then you won’t have much of a useful class left I would think. The real solution is to tell them this is how we do it, and enforce it.
Keep in mind, if you mark them as Protected, you may not have access to the method you want outside of the inheriting class.
Example: (assume myClass inherits Database, which has a protected method called Execute and MyClass is not overriding that method)
$myClass->Execute(...);
If Execute is located in Database and marked as protected, I can’t do the above statement. At least not without overriding it inside MyClass.
Your class should use the appropriate scope accordingly for each method. If it needs to be accessible publicly, so it can be called, mark it as such. If it will only be used internally within another class, mark it as such. If it will only be internally to its own class, mark it as such.
Making your class abstract prevents everyone from doing $myDatabase = new Database(); so that solves your initial problem. However, since you have a few static classes, those need changed to NOT be static. Keep the level of scope you have, as you’ve already solved problem around it being instantiated.
This is exactly what I DO NOT want…I do not want anyone to inherit the Database class directly, Since the ORM.php class extends the Database class, everyone SHOULD extend the ORM class and then play with its methods…
<?php
class Database {
public function __construct() {
if( !isset($this->extended) || false === $this->extended ) {
die('Cannot extend directly');
}
}
}
class ORM extends Database {
public $extended = true;
}
// Goog class
class good extends ORM {
}
// Bad class
class bad extends Database {
}
echo 'Good<br />';
$good = new good;
echo 'Bad<br />';
$bad = new bad;
?>
But easy to overcome, all I have to do is create that variable in bad and it works again. To put it blunt, you can’t stop others from directly inheriting Database. All you can do is warn against it, hold code reviews to ensure it doesn’t happen, and when it does, talk with the person who did it (maybe they had a good reason to do it).
To put it blunt, you can’t stop others from directly inheriting Database
It is true, as a programmer, you can inherit every class you want.
Also, you can open the class and remove the protection OR create a fopen-fwrite and rewrite the class content if you are stupid enough.
All you can do, as an architect, is (as you did say) to add a simple “warning, this is not a good practice”.