Daily Shaarli

All links of one day in a single page.

March 15, 2015

PHP Fun: Exceptions

Ils sont sympas sur le tchat PHP de SO.

Quelques amis m'ont donné des exemples de codes à propos des exceptions en PHP, que j'ai, un peu trop souvent rechigné.

C'est bô. Jugez-en par vous même:

http://3v4l.org/NJJjO

function (╯°□°)╯︵┻━┻(){throw new ┻━┻;}
class ┻━┻ extends Exception {public function construct() {parent::construct("Please respect tables! ┬─┬ノ(ಠ_ಠノ)");} public function __toString(){return "┬─┬";}}
// try/catch
try { (╯°□°)╯︵┻━┻ (); } catch ( ┻━┻ $niceguy) {echo $niceguy->getMessage();}
// ok now lets see an uncaught one
(╯°□°)╯︵┻━┻
();

// Output:
Please respect tables! ┬─┬ノ(ಠ_ಠノ)
Fatal error: Uncaught ┬─┬

Et http://3v4l.org/TkNpc

class JeromeException extends Exception
{
protected $boobies = [];

function __construct($message = null, $code = 0, Exception $previous = null, $arrayOfBoobies = [])
{
    $this->boobies = $arrayOfBoobies;
}

function getTraceEx()
{
    return $this->getTrace() + ['boobies' => $this->boobies];
}

}

function jeromeIsExceptional()
{
try {
throw new JeromeException('herro', 0, null, ['34B', '32C', '36D']);
}
catch (JeromeException $e) {
var_dump($e->getTraceEx());
}
}

jeromeIsExceptional();