Fatal error</b>: Call to undefined function [closed]
if i have this structure:
function one () {
if (4>3) {
return true;
}
else
return false;
}
function two () {
if (one()) {
echo ("ok");
}
else
echo("first function have a problem");
}
this works without any problem.
but now, if i have:
class all {
function one () {
if (4>3) {
return true;
}
else
return false;
}
public function two ()开发者_开发百科 {
if (one()) {
echo ("ok");
}
else
echo("first function have a problem");
}
}
$val = new all();
$val -> two ();
i receive: Fatal error: Call to undefined function one (); Why?
thanks
In PHP, you must explicitly say that you are calling an object's method. In some languages it is implicit; in PHP it is not.
You need to use $this
:
if ($this->one()) {
See the manual. As other answers have said, if you omit $this
the function call is treated as a call to a global function.
There, won't complain anymore about fatal errrzz:
class all {
function one () {
if (4>3) {
return true;
}
else
return false;
}
public function two () {
if ($this->one()) {
echo ("ok");
}
else
echo("first function have a problem");
}
}
$val = new all();
$val->two();
精彩评论