开发者

Fatal error</b>: Call to undefined function [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜