0

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

1

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.