如何在类中的函数中调用函数?
我的代码看起来像这样(例如)
class Test
{
echo $this->unique();
public function unique($unique = FALSE)
{
function random()
{
return 1;
}
while($unique === FALSE)
{
return $this->random();
$unique = TRUE;
}
}
}
但是我收到以下错误: 调用未定义的方法Test :: random()
答案 0 :(得分:1)
不是$this->random();
,只是random()
。
你的嵌套函数不是Test
类的方法,它只是一个函数,是你声明它的方法的本地函数。
请注意,您的代码似乎存在一些非常严重的缺陷。
此代码不起作用; while循环只执行一次,它只执行return
语句。程序流程永远不会到达$unique = TRUE
行。
while($unique === FALSE)
{
return $this->random();
$unique = TRUE;
}
答案 1 :(得分:0)
只需使用random();
class Test
{
echo $this->unique();
public function random()
{
return 1;
}
public function unique($unique = FALSE)
{
while($unique === FALSE)
{
return random();
$unique = TRUE;
}
}
}
答案 2 :(得分:0)
只需这样做:
这是一个示例代码:
这是你当前的主要功能:
function parentFuntion()
{
childFuntion();
}
这是您尝试从主要功能
调用的功能function childFuntion()
{
}