我有一个类方法内的函数.
在方法中我可以参考,$this但我不能在函数中.它将返回此错误:
致命错误:在第78行的/var/www/john/app/views/users/view.ctp中不在对象上下文中时使用$ this
这是我的意思的一个例子:
class View
{
var $property = 5;
function container()
{
echo $this->property;
function inner()
{
echo "<br/>Hello<br/>";
echo $this->property;
}
inner();
}
}
$v = new View();
$v->container();
Run Code Online (Sandbox Code Playgroud)
你可以在这里进行测试pastie
是否有工作要做到这一点?
我知道我可以将$ this作为参数传递给你,但还有其他方法吗?使用global $this也会出错.
如果你好奇我为什么需要这个,那因为我的方法是MVC模型中的一个视图(或者似乎 - 我正在使用Cake),并且在视图中我需要使用一个函数,并且在函数中我需要引用到$this.
不要在另一个函数中创建函数,试试这个:
class View {
var $property = 5;
function container() {
echo $this->property;
$this->inner();
}
function inner() {
echo "<br/>Hello<br/>";
echo $this->property;
}
}
Run Code Online (Sandbox Code Playgroud)