我可以从另一个函数返回一个返回值吗?

san*_*ruz 0 php oop

我有一个这样的课:

class Example {

    private function _test($value)
    {
         if ($value == 'xyz') return FALSE;
    }

    public function check()
    {
        $this->_test('xyz');

        // more code follows here...
    }

}
Run Code Online (Sandbox Code Playgroud)

基本上我想做的是从方法_test()中"冒泡"返回值FALSE作为方法check()的实际返回值,以便调用

$this->_test('xyz');
Run Code Online (Sandbox Code Playgroud)

将返回FALSE

return $this->_test('xyz');
Run Code Online (Sandbox Code Playgroud)

不起作用,因为如果值与'xyz'不匹配,我不想返回.

这有可能吗?

Pri*_*ner 5

我不理解你正在寻找什么,所以如果这不是你的意思,请解释一下你想要的更多.

public function check()
{
    if($this->_test('xyz') === FALSE){
         return FALSE;
    }

    // more code follows here...
}
Run Code Online (Sandbox Code Playgroud)

  • 如果你*只想在某个条件成立时返回,你别无选择,只能使用某种`if`结构. (2认同)