我有自己的類foo,/src/Utility/foo.php只要我添加,就可以在任何 CakePHP 腳本中訪問(wèn)它use App\Utility\foo;- 到目前為止,這是可行的。$this將調(diào)用者實(shí)例的對(duì)象傳遞給 PHP 構(gòu)造函數(shù)通常不會(huì)出現(xiàn)問(wèn)題。為了獲得準(zhǔn)確的實(shí)例名稱,$this我使用 . 在類的構(gòu)造函數(shù)中檢索了它get_class($this)。這返回AdminLTE\View\AdminLTEView我用 實(shí)例化我的自定義類$fooInstance = new foo($this);。我的類的構(gòu)造函數(shù)如下所示:public function __construct(AdminLTE\View\AdminLTEView $appThis)使用上述語(yǔ)法會(huì)導(dǎo)致此錯(cuò)誤:傳遞給 App\Utility\foo::__construct() 的參數(shù) 1 必須是 App\Utility\AdminLTE\View\AdminLTEView 的實(shí)例,給定 AdminLTE\View\AdminLTEView 的實(shí)例當(dāng)我沒(méi)有在構(gòu)造函數(shù)中設(shè)置類型時(shí),$this嘗試此命令時(shí)會(huì)收到以下 CakePHP 錯(cuò)誤消息$appThis->request->getAttribute('identity');:無(wú)法找到 requestHelper。好的,我誤解了什么,我錯(cuò)過(guò)了什么,正確的語(yǔ)法如何,以便我可以$this在自定義類中使用調(diào)用者類?
在自定義類中訪問(wèn) CakePHP $this
慕婉清6462132
2023-09-08 22:00:11