0

I throw an exception like this:

public function findRole($role)
{
    if(!is_string($role)){
        throw new \InvalidArgumentException(
            sprintf('Role should be a string, %s given.', gettype($role))
        );
    //...
    }

I have seen some exceptions like this and would like to do the same:

error: json_decode() expects parameter 1 to be string, array given.

Any chance I can automatically throw an exception like this so that the exception automatically outputs the name of the function and the invalid argument number for me?

2
  • You need some mechanism to catch the exception at a higher level and return it to the Ajax call Commented Feb 12, 2013 at 14:18
  • I see, so no way to do this at this level then. I am using the symfony2 framework, so maybe there is a way to do that at a higher level? Commented Feb 12, 2013 at 14:21

3 Answers 3

1

Those errors you want are printed automatically by PHP and probably handled nicely with a set_error_handler function. There's no way you can simulate the same behavior yourself (possibly without nonsense hacks). Therefore you are forced to go with your exception way.

There's an exception that you should be aware of: type hinting; that can only be used with arrays, classes, objects and callables (functions):

public function acceptArray(array $array);
public function acceptObject(object $o);
public function acceptClass(MyClass $o);
public function acceptCallback(callable $f);

These functions if called with any other type of variable will complain almost like the specific error you posted.

The hacks I was talking about earlier might include redefining every type yourself:

class Int {...}
class String {...}
class Float {...}
class Bool {...}

and then use it like that:

$bool = new Bool(true);
acceptString($bool); // public function acceptString(String $s);

will trigger an error. But that's just not how PHP was supposed to work. Therefore I still suggest you to go with your initial idea.

Sign up to request clarification or add additional context in comments.

2 Comments

That's awesome help @Jeffrey. I use type hinting a lot but as you mention it can only be called on certain types and not string, integer, etc.. Many thanks.
Awesome, everything makes sense now. I like the hacks you mention. I'll stay with my initial idea or create a generic template as mentioned by @fab. Many thanks for this sir. (Now I am not sure which answer to accept, I'll wait a little bit and see the number of up-votes.)
1

Not automatically, but you could make kind of a generic template, for example like that:

if(!is_string($role)) {
    throw create_invalid_argument_exception(__METHOD__, 1, 'string', $role);
}



function create_invalid-argument_exception($method, $argNo, $expectedType, $actualValue) {
    return new \InvalidArgumentException(
        sprintf(
            '%s expects parameter %d to be %s, %s given.',
            $method, $argNo, $expectedType, gettype($actualValue)
        )
    );
}

1 Comment

I like that very much @fab.
-1

For catching exceptions, you must use the construction:

try{
    /** you code here  */
}catch(Exception $e){
    /** convert $e to json and output */
}

And wrap you main function by it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.