Checking the result of the function

In the Run() method, how do you check that setName() and setLastName() worked correctly before executing getFullName()?


Answer 1, authority 100%

In this case, it is enough to check the fields $_nameand $_lastNamefor emptiness

And, again, it’s quite difficult to imagine a class with just such a construction…

If the class were “normal”. those. something like:

class MyClass
{
    private $_name;
    private $_lastName;
    public function __construct($name, $lastName) {
        //         
        // .. - 
        if( is_string($name) && is_string($lastName) ) {
            $this->_setName($name);
            $this->_setLastName($lastName);
        }
        else {
            // -  ...
            //      
        }
    }
    //    run   ...
    /*
    public function run()
    {   
        $this->_setName();
        $this->_setLastName();
        $this->_getFullName();
    }
    */
    private function _setName($name)
    {
        //   , 
        //  -    
        //     
        $name = ucfirst($name); //     
        $this->_name = $name;
    }
    private function _setLastName($lastName)
    {
        //  if (isset($this->_name))
        //      
        $lastName = ucfirst($lastName);
        $this->_lastName = $lastName;
    }
    //  - 
    //         get 
    //   ...
    //    -  100%   
    private function getFullName()
    {
        //      :)
        return $this->_name .' ' . $this->_lastName;
    }
}
//      
$user = new MyClass('', '');
echo $user->getFullName();

Now I’ll explain why I rewrote the class.. The reason is that your example makes absolutely no practical sense, in your example we have only 1 public method that initializes the fields explicitly, i.e. not even variables are initialized, but fixed values ​​.. Of course, I understand that the example is abstract, but nevertheless .. The run method cannot cause an error by definition. So I rewrote your example a bit.

Again, it’s important to understand – if you can’t (don’t want to) use exceptions – use simple ifs…

If the class is quite large and it can have a lot of errors, then you need to find out about them somehow, for example, you can initialize the $_errorsfield and the hasErrors()method >which will count the length of the array $_errorsand, if it is greater than 0n, return true, and the public method getErrors()which simply returns errors . Then you get something like:

$user = new MyClass('', '');
if(!$user->hasErrors()) { //  
    //    
}
else {
    // -  ...
    $errors = $user->getErrors(); //  
    //   ...
}

Answer 2

It all depends on what you mean by correctness. But you can use exceptions, see php.net/manual/en/language.exceptions.php, then your code will look like this:

public function run() {
    $e = FALSE;
    try {   
        $this->_setName();
        $this->_setLastName();
    }
    catch (Exception $e) {
        // - 
    }
    if ($e === FALSE) {
        //   OK
        $this->_getFullName();
    }
}

Of course, in _setNameand _setLastNameif something is wrong, then do:

throw new Exception('Error message here');