Hi,
do you guys use typehinting, if so: why is it useful to you?
I found that while it produces a "catchable fatal error", this error, in fact, can not be caught in a try/catch block.
When I code a simple check and an exception myself I actually CAN catch it...which seems a better approach, what do you think?
(Here is what the guys at php.net have to say about it: linkage
Which imnsho is missing the point. If it is called "catchable" it should be or if it shouldn't be catchable, then it shouldn't be called so.)
Example:
<?php
$tht = new TypeHintingTest;
try {
$tht->setMyHintedVar(5); //so called "catchable exception" is thrown, but catch block won't get executed
echo $tht->getMyHintedVar(); //property is even set to 5 and code execution continues!
$tht->setMyStandardVar(5); //as intended, execution is transferred to catch block
echo $tht->getMyStandardVar(); //this won't execute
} catch (Exception $e) {
throw new exception($e->getMessage()); //just passing it on to the debugger
exit;
}
class TypeHintingTest
{
private $_myHintedVar = null;
private $_myStandardVar = null;
public function setMyHintedVar(mysqli $mysqli)
{
$this->_myHintedVar = $mysqli;
}
public function getMyHintedVar()
{
return $this->_myHintedVar;
}
public function setMyStandardVar($mysqli)
{
if ($mysqli instanceof mysqli) {
$this->_myStandardVar = $mysqli;
} else {
throw new exception ('Parameter of wrong type. Expected instance of mysqli, got ' . gettype($mysqli));
}
}
public function getMyStandardVar()
{
return $this->_myStandardVar;
}
}
?>
Bjom