正确设置控制器类

时间:2017-01-04 09:02:23

标签: php class oop pdo controllers

我会尝试以最好的方式解释自己。我试图从PHP开始使用OOP。我是这样开始的,我做了一个带有构造的“主控制器”,我启动了所有其他控制器。我这样做是因为我希望能够与控制器共享数据。

class clsController{
    public function __construct($smarty){               
        if (is_object($smarty)) {
            $this->o_smarty = $smarty;
        } else {
            throw new Exception("Smarty not set!");
        }

        $this->o_clsSharedAdsController = new clsSharedAdsController($this->o_smarty);
        $this->o_clsSharedUserController = new clsSharedUserController($this->o_smarty);
    } 
}

到目前为止,在我看来它看起来很漂亮。现在问题是当我在类clsSharedUserController中时,它看起来像这样:

class clsSharedUserController extends clsController{
    // construct
    public function __construct($smarty) {              
        if (is_object($smarty)) {
            $this->o_smarty = $smarty;
        } else {
            throw new Exception("Smarty not set!");
        }
    }
}

我无法访问clsSharedAdsController中的函数。控制器看起来像这样。

class clsSharedAdsController extends clsController{
    // construct
    public function __construct($smarty) {              
        if (is_object($smarty)) {
            $this->o_smarty = $smarty;
        } else {
            throw new Exception("Smarty not set!");
        }
    }

    // ad details used for messages
    public function getAdDetailsForMessage($ads_id){
        echo $ads_id;
    }

}

我试图以下列方式访问它

class clsSharedUserController extends clsController{
    // construct
    public function __construct($smarty) {              
        if (is_object($smarty)) {
            $this->o_smarty = $smarty;
        } else {
            throw new Exception("Smarty not set!");
        }
    }

    // ad details used for messages
    public function getUserReviews($userReviews){
        $this->o_clsSharedAdsController->getAdDetailsForMessage(1);
    }

}

说实话,我希望得到我的回音,但我收到了这个错误:

Fatal error: Uncaught Error: Call to a member function getAdDetailsForMessage() on null in /home/vhosts/gamermarket.nl/httpdocs/includes/controllers/shared/clsSharedUserController.php:124 
Stack trace: 
#0 /home/vhosts/gamermarket.nl/httpdocs/includes/controllers/shared/clsSharedUserController.php(68): clsSharedUserController->getUserReviews(Array)
#1 /home/vhosts/gamermarket.nl/httpdocs/includes/controllers/clsReviewController.php(17): clsSharedUserController->getUserReviewsFromUsersId('0000000001') 
#2 /home/vhosts/gamermarket.nl/httpdocs/reviews.php(10): clsReviewController->getReviews() 
#3 {main} thrown in /home/vhosts/gamermarket.nl/httpdocs/includes/controllers/shared/clsSharedUserController.php on line 124

我正在使用PDO类来查询我们设置的数据,就像这个clsController一样。我很可能错过了一些正确的设置逻辑,以便在不创建新实例的情况下在所有类中共享数据。你应该能够只创建一个类的实例并在其他类中共享它,而不是?

3 个答案:

答案 0 :(得分:0)

我认为你的主类不应该在构造

中新建一个子类

在clsSharedUserController

class clsController{
    public function __construct($smarty){               
        if (is_object($smarty)) {
            $this->o_smarty = $smarty;
        } else {
            throw new Exception("Smarty not set!");
        }
    } 
}

在clsSharedUserController

class clsSharedUserController extends clsController{

    protected $o_clsSharedAdsController;

    // construct
    public function __construct($smarty) {              
        parent::__construct($smarty);
        $this->o_clsSharedAdsController = new clsSharedAdsController($smarty);
    }

    // ad details used for messages
    public function getUserReviews($userReviews){
        $this->o_clsSharedAdsController->getAdDetailsForMessage(1);
    }
}

在clsSharedAdsController

class clsSharedAdsController extends clsController{
    // construct
    public function __construct($smarty) {              
        parent::__construct($smarty);
    }

    // ad details used for messages
    public function getAdDetailsForMessage($ads_id){
        echo $ads_id;
    }

}

答案 1 :(得分:0)

您必须先创建clsSharedAdsController类的实例,然后才能使用其方法。

 o_clsSharedAdsController = new clsSharedAdsController($smarty);
 o_clsSharedAdsController->getAdDetailsForMessage(1);

答案 2 :(得分:0)

如果你想在clsSharedAdsController中使用一些clsSharedUserController的方法 并在clsSharedUserController中使用一些clsSharedAdsController方法 你需要重新设计课程

在clsController中

class clsController{

    protected $o_smarty;

    public function __construct($smarty){               
        if (is_object($smarty)) {
            $this->o_smarty = $smarty;
        } else {
            throw new Exception("Smarty not set!");
        }
    } 

    public function getSharedUser()
    {
        return new clsSharedUserController($this->o_smarty);
    }

    public function getSharedAds()
    {
        return new clsSharedAdsController($this->o_smarty);
    }
}

在clsSharedUserController

class clsSharedUserController extends clsController{

    // construct
    public function __construct($smarty) {              
        parent::__construct($smarty);
    }

    // ad details used for messages
    public function getUserReviews($userReviews){
        $sharedAds = $this->getSharedAds();
        $sharedAds->getAdDetailsForMessage(1);
    }
}

在clsSharedAdsController

class clsSharedAdsController extends clsController{
    // construct
    public function __construct($smarty) {              
        parent::__construct($smarty);
    }

    // ad details used for messages
    public function getAdDetailsForMessage($ads_id){
        echo $ads_id;
    }

    //use the method in share user class
    public function someMethod(){
        $sharedUser = $this->getSharedUser();
        //TODO
    }

}