Reputation: 12287
I am reading that the best way to use a class properties (database class
) within another class (html class
) is to create an instance of that class within the class (html
). As to why, I am not sure...but anyway.
How is this done? I have two scenarios, to see which one(s) are correct and which ones are wrong...
Scenario A
require( database.php );
class html(){
private static $db = null;
private static $page = null;
public function __construct($id){
self::bootstrap($id);
}
public static function bootstrap($id){
self::$db = new database();
self::$page = $db->page($id);
return self::$page;
}
}
//$page = new html('hello-world');
//print $page;
print html::bootstrap('hello-world');
Scenario B
//Class autoloader
spl_autoload_register(function ($class) {
include $_SERVER['DOCUMENT_ROOT'] . '/class/' . $class . '.php';
});
//Scenario B code
class html(){
private static $page = null;
public static function bootstrap($id){
self::$page = database::page($id);
return self::$page;
}
}
print html::bootstrap('hello-world');
Perhaps you have a different scenario that's appropriate, if these are the wrong approach
Upvotes: 0
Views: 68
Reputation: 687
I would say no scenario is wrong, but scenario B is more appropriate. Since page
was designed as static method in database
class, that informs the intentional usage of the method.
Upvotes: 1