更新时间:2023-11-19 19:15:46
您需要了解 scope 。您已在 beforeFilter()
作用域中初始化了一个变量,然后尝试在 showMe
作用域中使用它。两个是完全不同的。
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();
}
另一点要注意的是,您可以使用 App :: uses()
方法来加载类。根据你的命名它会工作。 (类是以这种方式加载的)
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');