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.