简体   繁体   中英

PHP Class Function Calling

I'm writing my own php class and I have several functions within that class. But am I not allowed to call a function from another function within the same class? Something like this:

class my_Class {
    function one($arg) {
        //does something
    }

    function two($var) {
        $receive = one($var);
    }
}

I tried something like this and it spat out an error saying:

Fatal error: Call to undefined function one()

What am I doing wrong?

Change it to this:

function two($var) {
      $receive = $this->one($var);
 }

Review the PHP OOP reference: http://www.php.net/manual/en/language.oop5.php
The $this keyword is always required.

It should be

class my_Class {
    function one($arg) {
        // does something
    }
    function two($var) {
        $receive = $this->one($var);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM