1

Let's say I have a class that accepts an array of SomeClass when instantiated, for example:

class ConfigurableClass
{
    public function __construct( array $config = array() )
    {
        foreach( $config as $param )
        {
            if( !$param instanceof SomeClass )
            {
                $type = gettype($param);
                $line = ? // How can I retrieve this?
                throw new Exception("ConfigurableClass config parameters must be of type SomeClass, $type given in line $line");
            }
        } 
    }
}

And let's say this class is instantiated in a different file with an array that has one element of the wrong type, for example:

$instance = new ConfigurableClass(array(
   new SomeClass(),
   new SomeClass(),
   new SomeClass(),
   'Ooops!',       // String type, line 5
   new SomeClass()
));

How can I throw an error message specifying the line number where the wrong object type was inserted? In this case, the appropriate message should read:

"ConfigurableClass config parameters must be of type SomeClass, string given in line 4"

Keep in mind that this is just an example. The real class can accept a very large and complicated array. Knowing the line number in that case can be very useful.

9
  • php.net/manual/en/language.constants.predefined.php but to handle your particular case, you'd probably need to work your way up the stack backtrace. Commented Jan 15, 2015 at 21:32
  • You shouldn't need this, just let the user know that they passed in an incorrect type, you could even give them which argument was incorrect by utilizing func_get_args php.net/manual/en/function.func-get-args.php Commented Jan 15, 2015 at 21:33
  • @ZachSpencer What if I want to make their life easier? Imagine that this class can accept very large configuration arrays, wouldn't it be useful then? Commented Jan 15, 2015 at 21:35
  • @MarcB, can you post an example? Commented Jan 15, 2015 at 21:36
  • php.net/manual/en/function.debug-backtrace.php Commented Jan 15, 2015 at 21:37

2 Answers 2

2

the simplest maybe

class MyException extends Exception {
  function __construct($message, $file, $line) {
    $this->file = $file;
    $this->line = $line;
  }
}

throw new MyException('error message', 'foo.php', 20220501);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I confirm, it is simple and it works well
1

There is no way to determine the line (5 in your case) because it is given by the end of the caller instruction, that is, where the semicolon is located (7 in your case).

However you can get a good approximation by showing that line number and the wrong parameter number.

<?php
class ConfigurableClass
{
    public function __construct( array $config = array() )
    {
        $counter = 0;

        foreach( $config as $param )
        {
            if( !$param instanceof SomeClass )
            {
                $type = gettype($param);
                $line = debug_backtrace()[0]['line'];
                throw new Exception("ConfigurableClass config parameter[$counter] must be of type SomeClass, $type given in line $line");
            }

            $counter++;
        } 
    }
}

The message will be something like:

Fatal error: Uncaught exception 'Exception' with message 'ConfigurableClass config parameter[3] must be of type SomeClass, string given in C:\your\folder\origin_file.php at line 12' in C:\your\folder\ConfigurableClass.php on line 15

If, in the message, you want to avoid the reference to the ConfigurableClass file and line, you must to create your own derived exception class and overwrite the __toString() method.

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.