Instantiating a vendor class in class constructor

匆匆过客 提交于 2019-12-02 09:27:22

You need to learn about scope. You have initialised a variable in the beforeFilter() scope and then trying to use it in the showMe scope. The two are completely different.

You can make a variable that is scoped to the entire class, normally called a property...

function beforeFilter() {
   $this->fancyVendor = new fancyVendor();
   $this->fancyVendor->setValue("12");
}

function showMe() {
   echo $this->fancyVendor->getValue();
}

Another point to note is that you can use the App::uses() method to load the class. According to your naming it would work. (class is lazy loaded this way)

App::uses('fancyVendor', 'Vendor');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!