Vishal Kumar Sahu
Vishal Kumar Sahu

Reputation: 1396

Iterate through return of Methods in a class

This is part of my code... Where I have to get return value of multiple methods and proceed further. All the methods return true or false. I find this deep nesting of 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.';
    }

Is there any way to do like this-

$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;
}

I am getting no clue to iterate through return of the Methods of a class.

Upvotes: 1

Views: 54

Answers (3)

Niek van der Maaden
Niek van der Maaden

Reputation: 482

This is were the dynamic language of PHP comes into play. You can do the following:

<?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();

The above is an example ofcourse.

Upvotes: 2

ConstantineUA
ConstantineUA

Reputation: 1051

PHP easily allows dynamic method invocation. You can loop through the list of your methods calling them consequentially and processing results on the each step.

$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;
}

Upvotes: 3

Anwar
Anwar

Reputation: 4246

You could use the power of try{} catch() {} to avoid pyramide checking like following :

<?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;
    }
?>

Upvotes: 1

Related Questions