以正确的方式设置控制器 类
Setting up controller classes the correct way
我会尽量用最好的方式来解释我自己。我正在尝试从 PHP 中的 OOP 开始。我以这种方式开始,我用一个构造创建了一个 "main controller",我在其中启动了所有其他控制器。我这样做是因为我希望与控制器共享数据。
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);
}
}
到目前为止,我认为它看起来还不错。现在事情是当我在 class 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 class 来获取数据,它们的设置方式与此 clsController 相同。我可能缺少一些正确设置的逻辑,以便在所有 classes 中共享我的数据,而无需一直创建新实例。您应该可以创建一个 class 的实例并与其他 class 共享,不是吗?
我认为你的 main class 不应该是 new child class inside construct
在 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;
}
}
您必须先创建 clsSharedAdsController class 的实例,然后才能使用它的方法。
o_clsSharedAdsController = new clsSharedAdsController($smarty);
o_clsSharedAdsController->getAdDetailsForMessage(1);
如果你想在 clsSharedAdsController 中使用一些 clsSharedUserController 的方法
并在 clsSharedUserController 中使用一些 clsSharedAdsController 的方法
您需要重新设计 class
在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
}
}
我会尽量用最好的方式来解释我自己。我正在尝试从 PHP 中的 OOP 开始。我以这种方式开始,我用一个构造创建了一个 "main controller",我在其中启动了所有其他控制器。我这样做是因为我希望与控制器共享数据。
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);
}
}
到目前为止,我认为它看起来还不错。现在事情是当我在 class 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 class 来获取数据,它们的设置方式与此 clsController 相同。我可能缺少一些正确设置的逻辑,以便在所有 classes 中共享我的数据,而无需一直创建新实例。您应该可以创建一个 class 的实例并与其他 class 共享,不是吗?
我认为你的 main class 不应该是 new child class inside construct
在 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;
}
}
您必须先创建 clsSharedAdsController class 的实例,然后才能使用它的方法。
o_clsSharedAdsController = new clsSharedAdsController($smarty);
o_clsSharedAdsController->getAdDetailsForMessage(1);
如果你想在 clsSharedAdsController 中使用一些 clsSharedUserController 的方法 并在 clsSharedUserController 中使用一些 clsSharedAdsController 的方法 您需要重新设计 class
在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
}
}