发布于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黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 php黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-4
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!