use*_*712 0 php oop cakephp cakephp-1.2
我正在学习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";}
}
}
Run Code Online (Sandbox Code Playgroud)
如果可以,请帮忙.
编辑原因:
你好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"
}
}
Run Code Online (Sandbox Code Playgroud)
class TestingController extends AppController {
var $myVariable="hi there";
function hello(){
if($newUser){echo $this->myVariable;}
}
function world(){
if($newUser=="old"){$this->myVariable="hi my friends";}
}
}
Run Code Online (Sandbox Code Playgroud)
(注意,当你调用方法时,$ newUser是未定义的).
你应该读这个:http://php.net/manual/en/language.oop5.php
归档时间: |
|
查看次数: |
7231 次 |
最近记录: |