Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: $e obj?

  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts

    $e obj?

    Why did they use $e as the ojb? How would I know what letter I should use? What is the terminology for this variable? (Object variable?)
    And why did they use "return $errorMsg;" instead of "echo $errorMsg;"?

    PHP Code:
    class customException extends Exception {
                    public function 
    errorMessage() {
                        
    $errorMsg 'Error on line ' $this->getLine() . ' in ' $this->getFile() . ': <b>'
                        
    $this->getMessage() . '</b> is not a valid E-Mail address';
                        return 
    $errorMsg;
                    }
                }
                
    $email "someone@example...com";
                try {
                    if(
    filter_var($emailFILTER_VALIDATE_EMAIL) === false) {
                        throw new 
    customException($email);
                    }
                }
                catch (
    customException $e) {
                    echo 
    $e->errorMessage();
                }

    Last edited by RonnyNishimoto; 07-20-2012 at 07:41 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    $e is typically used when issuing a catch. It stands for $exception.
    You can use anything you want, just don't overwrite anything in the meantime.

    As for the return, that is required in order to issue it with the echo $e->errorMessage().
    I wouldn't do that myself, I'd just pass the message off to the parent class to build, and if I want to display the line and file I'd invoke that information during the catch. To me I consider the stack trace for more valuable anyway.
    A big part of exception handling is when the application is given something, it determines something is not right, but doesn't know how to deal with it. So you throw an exception as a way for the underlying code to say I don't know what to do with it, so you deal with it. In this aspect, many of your exceptions need no body at all.
    PHP Code:
    class NotOwnerException extends RuntimeException
    {
    }

    class 
    LastOwnerException extends RuntimeException
    {
    }

    //...
    public function removeOwner($toRemove$authorizedBy)
    {
        if (!
    $this->isOwner($authorizedBy))
        {
            throw new 
    NotOwnerException();
        }
        if (
    count($this->getOwners()) <= 1)
        {
            throw new 
    LastOwnerException();
        }
        
    //. . .
    }

    try
    {
        
    $acl->removeOwner($toRemove$authorizedBy);
    }
    catch (
    NotOwnerException $ex)
    {
         print 
    'You are not an owner.';
    }
    catch (
    LastOwnerException $ex)
    {
         print 
    'You cannot remove the last owner.';

    For example. I typically only use messages if its ambiguous, so if instead I throw InvalidArgumentException for both scenarios, I'd add a message to indicate which argument threw the error.

  • Users who have thanked Fou-Lu for this post:

    RonnyNishimoto (07-20-2012)

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    85
    Thanks
    53
    Thanked 0 Times in 0 Posts
    Confusing but I understand better haha. I got an error for some reason when I tried changing both the $e's, but now it works for some reason. Thanks fou!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •