2

I am currently developing my PHP SDK to interact with a REST API. Let's say the API can create a 'car' model. To create a car I would write something like this...

$car = $api->create('car', array(
   'wheels' => 6,
   'color'  => 'blue'
));

If another developer downloads my SDK and tries to create a car model incorrectly and forgets to include required arguments. How can I throw an exception via the SDK to notify the developer of missing arguments, other than them seeing a PHP error like Warning: Missing argument 1 for BMW::create() which does not include many details.

4
  • Create a new exception class, extend it with Exception, and then throw it via throw new CarException Commented May 7, 2015 at 12:15
  • Throw an exception. The developer should handle this with a try catch. Commented May 7, 2015 at 12:16
  • 2
    php.net/manual/en/language.exceptions.php Go through this link Commented May 7, 2015 at 12:17
  • Where would I throw this exception? Commented May 7, 2015 at 12:17

3 Answers 3

6
function foo($bar, $baz) {
    if (!isset($bar, $baz)) {
        throw new InvalidArgumentException("Detailed description of what's wrong here");
    }
    ...
}

PHP will trigger a warning, but will still execute your function as usual (which is... oh well, let's not dwell on it). That means you can do your regular argument checking inside your function and throw exceptions or trigger_errors all you want in as much detail as you want.

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

7 Comments

In this case we have to turn off the warnings i think.
Why would you? You'll get a warning and an additional exception, but I'd hardly call this a problem worth turning off warnings for.
if i call the function like foo(1) then the warning message will be shown on the page.
Yes it will. And it should. What's wrong with that?
Of course on a production server your php.ini will have display_errors = off. It's still a good idea to trigger the warning.
|
0

Please go through this page...

http://php.net/manual/en/language.exceptions.php

And try something like following

<?php
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    return 1/$x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo "Hello World\n";
?>

Comments

0

As of PHP 7.1, invoking user-defined functions with too few arguments will result in an Error exception. You do not need to implement the functionality yourself.

https://www.php.net/manual/en/migration71.incompatible.php

1 Comment

The create method accepts two arguments, so PHP won't throw an Error exception if both are provided; it also won't know how many parameters are supposed to be in the array or what type they are, so it's still up to the developer to check that the caller provides the correct values and doesn't, for example, forget to specify how many wheels the car should have.

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.