0

我想更好地了解对函数的调用如何在 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();

在 required.php 中:

function required_func(){

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

我想要做的是让 required_func() 使用 $customerInfo 运行 do()。所以本质上:如何从 require_once 文件中包含的另一个函数调用一个类公共函数?我什至在这里遥遥无期?

谢谢你的帮助

4

1 回答 1

1

$this 不在函数 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;
}
于 2013-04-01T13:21:41.710 回答