Code confusion while extending PHP exception class -
this question related extending php exception class , there many similar questions 1 different.
i trying extend php exception class can add values exception message. below code.
class om_exception extends exception { public function __construct($message, $code = 0, exception $previous = null) { $message = $this->_getmessage($message); parent::__construct($message, $code, $previous); } protected function _getmessage($message) { $exception = '<br />'; $exception .= '<b>exception => </b>'.$message.'<br />'; $exception .= '<b>class => </b>'.get_called_class().'<br />'; $exception .= '<b>error line => </b>'.$this->getline().'<br />'; $exception .= '<b>error file => </b>'.$this->getfile().'<br />'; return $exception; } }
this works fine. , problem.
since calling functions getline()
, getfile()
of parent class before calling constructor shouldn't return blank values? if not error?
but works fine , output described below.
exception => hello.. class => om_controller_exception error line => 30 error file => c:\users\jay\projects\order-manager\application\modules\default\controllers\logincontroller.php
can please me understand why behavior? how can use class methods before initializing class?
the constructor called on newly created object, object , it's properties , methods exists when constructor called. example should make pretty clear:
<?php class testparent { protected $protectedstuff = 1; public function __construct($intnumber) { $this->protectedstuff = $intnumber; } } class testchild extends testparent { public function __construct($intnumber) { echo get_class() . '<br />'; // testchild echo get_parent_class() . '<br />'; // testparent $this->printstuff(); // 1 parent::__construct($intnumber); $this->printstuff(); // 42 } public function printstuff() { echo '<br />the number now: ' . $this->protectedstuff; } } $objchild = new testchild(42);
result
testchild
testparent
the number now: 1
the number now: 42
Comments
Post a Comment