Php 迭代类中方法的返回

Php 迭代类中方法的返回,php,methods,Php,Methods,这是我代码的一部分。。。在这里,我必须获得多个方法的返回值并继续。所有方法都返回true或false。 我发现这是if的深巢 if($this->user_exists()){ if($this->check_password()){ if($this->check_user_type()){ if($this->initiate_session()){

这是我代码的一部分。。。在这里,我必须获得多个方法的返回值并继续。所有方法都返回true或false。 我发现这是if的深巢

    if($this->user_exists()){
        if($this->check_password()){
            if($this->check_user_type()){
                if($this->initiate_session()){
                   ...
                   and so on...
                   ...
                }else{
                    return false;
                    $this->error_array[] = 'Problem in initiating session.';
                }
            }else{
                return false;
                $this->error_array[] = 'User type could not be determined.';
            }
        }else{
            return false;
            $this->error_array[] = 'Wrong password.';
        }
    }else{
        return false;
        $this->error_array[] = 'User does not exist.';
    }
有什么办法可以这样做吗-

$checking_steps = array('user_exists','check_password','check_user_type','initiate_session',...);

$i = 0;
foreach($checking_steps as $method){
    ++$i;
    $return_of_the_method
    if(return_of_the_method === false){
        break;
    }
}

if(count($checking_steps) === $i && empty($this->error_array)){
    return true;
}else{
    return false;
}

我不知道如何迭代返回类的方法。

PHP很容易允许动态方法调用。您可以在方法列表中循环,依次调用它们,并在每个步骤上处理结果

$checking_steps = array(
    'user_exists' => 'User does not exist.',
    'check_password' => 'Wrong password.',
    'check_user_type' => 'User type could not be determined.',
    'initiate_session' => 'Problem in initiating session.',
);

foreach ($checking_steps as $method => $message) {

    $result = $this->$method();

    if ($result === false) {
        $this->error_array[] = $message;

        break;
    }
}

if (empty($this->error_array)) {
    return true;
} else {
    return false;
}

PHP很容易允许动态方法调用。您可以在方法列表中循环,依次调用它们,并在每个步骤上处理结果

$checking_steps = array(
    'user_exists' => 'User does not exist.',
    'check_password' => 'Wrong password.',
    'check_user_type' => 'User type could not be determined.',
    'initiate_session' => 'Problem in initiating session.',
);

foreach ($checking_steps as $method => $message) {

    $result = $this->$method();

    if ($result === false) {
        $this->error_array[] = $message;

        break;
    }
}

if (empty($this->error_array)) {
    return true;
} else {
    return false;
}

这就是PHP的动态语言发挥作用的原因。 您可以执行以下操作:

<?php
class Steps
{        
    private $checking_steps = array('user_exists', 'check_password', 'check_user_type', 'initiate_session');

    public function doLogic()
    {
        $i = 0;
        foreach ($this->checking_steps as $method) {
            ++$i;
            $result = $this->{$method}();
            if ($result === false) {
                break;
            }
        }
    }

    private function user_exists()
    {
        return false;
    }
}

$class = new Steps();
$class->doLogic();

这是PHP的动态语言发挥作用的时候了。
您可以执行以下操作:

<?php
class Steps
{        
    private $checking_steps = array('user_exists', 'check_password', 'check_user_type', 'initiate_session');

    public function doLogic()
    {
        $i = 0;
        foreach ($this->checking_steps as $method) {
            ++$i;
            $result = $this->{$method}();
            if ($result === false) {
                break;
            }
        }
    }

    private function user_exists()
    {
        return false;
    }
}

$class = new Steps();
$class->doLogic();

您可以使用
的功能尝试{}catch(){}
来避免金字塔检查,如下所示:

<?php
    try
    {
        if( ! $this->user_exists() ) 
        {
            throw new Exception('User does not exist.');
        }
        else if( ! $this->check_password() )
        {
            throw new Exception('Wrong password.');
        }
        else if( ! $this->check_user_type() )
        {
            throw new Exception('User type could not be determined.');
        }
        else if( ! $this->initiate_session() )
        {
            throw new Exception('Problem in initiating session.');
        }
        else if( ! $this->my_other_function() )
        {
            throw new Exception('My other exception message.');
        }

        // all clear, do your job here ...
    }
    catch(Exception $e)
    {
        $this->error_array[] = $e->getMessage();
        return false;
    }
?>

您可以使用
的功能尝试{}catch(){}
来避免金字塔检查,如下所示:

<?php
    try
    {
        if( ! $this->user_exists() ) 
        {
            throw new Exception('User does not exist.');
        }
        else if( ! $this->check_password() )
        {
            throw new Exception('Wrong password.');
        }
        else if( ! $this->check_user_type() )
        {
            throw new Exception('User type could not be determined.');
        }
        else if( ! $this->initiate_session() )
        {
            throw new Exception('Problem in initiating session.');
        }
        else if( ! $this->my_other_function() )
        {
            throw new Exception('My other exception message.');
        }

        // all clear, do your job here ...
    }
    catch(Exception $e)
    {
        $this->error_array[] = $e->getMessage();
        return false;
    }
?>


你刚刚击败了我:)+1哇!有那么简单吗$方法()@常数你能稍微编辑一下吗?我想把它选为已接受。当然,我应该添加什么?我期待一个终止符和花括号。你付出的不止这些。。。谢谢你刚刚打败我:)+1哇!有那么简单吗$方法()@常数你能稍微编辑一下吗?我想把它选为已接受。当然,我应该添加什么?我期待一个终止符和花括号。你付出的不止这些。。。谢谢,我能做到,但这不是我想要的当然我能做到,但这不是我想要的我接受失败;),通过使用类而不是全局函数,我将把它保存在这里以备将来参考。是的,这是您方法的一个优点:-p我现在喜欢它,因为我喜欢OOPS。我接受我的失败;),通过使用类而不是全局函数,我将把它保存在这里以备将来参考。是的,这是您方法的一个优点:-p我现在喜欢它,因为我喜欢OOPS。