我正在学习 cakePHP 1.26。
我有一个控制器,它有两个功能。
我正在考虑使 $myVariable 成为全局变量,以便控制器中的两个函数可以共享它,但我不确定这是否是在 cakePHP 中声明全局变量的最佳方法:
class TestingController extends AppController {
var $myVariable="hi there";
function hello(){
if($newUser){echo $myVariable;}
}
function world(){
if($newUser=="old"){$myVariable="hi my friends";}
}
}
如果可以,请提供帮助。
编辑原因:
嗨,Aircule,
我对代码进行了一些更改并遵循了您的建议,但 myVariable 的值根本没有改变:
class TestingController extends AppController {
var $myVariable="hi there";
function hello(){
echo $this->myVariable;
}
function world(){
$this->myVariable="hi my friends";
}
function whatValue(){
echo $this->myVariable; // still output "hi there"
}
}