程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

以正确的方式设置控制器类

发布于2023-12-27 22:23     阅读(414)     评论(0)     点赞(30)     收藏(2)


我会尽力以最好的方式解释自己。我正在尝试从 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);
    }

}

老实说,我希望得到 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 相同。我可能缺少一些正确设置的逻辑,以便在所有类中共享我的数据,而无需始终创建新实例。您应该能够只创建一个类的 1 个实例并在其他类之间共享它,不是吗?


解决方案


如果您想在 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
    }

}


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.phpheidong.com/blog/article/550910/3bc47c75d08d121b290a/

来源:php黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

30 0
收藏该文
已收藏

评论内容:(最多支持255个字符)