在另一个函数中调用Class中的函数

时间:2013-04-01 13:15:29

标签: php oop

我想更好地了解如何在OOP中调用函数。我有以下样本:

class SomeClass {
    function __construct(){
        //run function do()
        //run function include()
        //run function run()
    }

    public function do($foo){
        //do some stuff
    }

    public function include(){
        require_once( CONSTANT . 'required.php' );
    }

    public function run(){
        required_func();
    }
}

$load_class = new SomeClass();

in required.php:

function required_func(){

    $customerInfo = "info";
    $customer = $this -> do($customerInfo); //--> This isn't right
    return $customer;
}

我尝试做的是让required_func()使用$ customerInfo运行do()。基本上:如何从require_once文件中包含的另一个函数调用类公共函数?我甚至在这里远程走上正轨吗?

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

$这不在函数required_func()

的范围内
class SomeClass {
    function __construct(){
        //run function do()
        //run function include()
        //run function run()
    }

    public function do($foo){
        //do some stuff
    }

    public function include(){
        require_once( CONSTANT . 'required.php' );
    }

    public function run(){
        required_func($this);
    }
}

$load_class = new SomeClass();

function required_func($customerObject){

    $customerInfo = "info";
    $customer = $customerObject->do($customerInfo);
    return $customer;
}