| 
	
 | 
 Posted by Markus L. on 03/20/05 15:31 
Am Freitag, den 18.03.2005, 15:23 -0500 schrieb Joseph 
CrawfordcodebowlatNOSPAMgmaildotc: 
> Hello Everyone, 
>  
> I am not sure if this is the expected results or not, however i think it 
> wouldnt be. 
>  
> i have a main exception class called SvEx that extends the built in 
> Exception class 
>  
> <?php 
> /** 
> * The SvEx class 
> * 
> * This is the exception class for the main Application 
> * 
> * @author Joseph Crawford Jr. <codebowl@gmail.com> 
> * @copyright Codebowl Solutions 2005 
> * @package SimonVolkov 
> * @subpackage Exceptions 
> * 
> **/ 
> class SvEx extends Exception { 
>  
>  const ERR_TYPE = "Unknown Error"; 
>  const ERR_DESC = "There has been an error. This error has been logged. 
> Please try again in a few minutes."; 
>  
>  
>  protected $_msg; 
>  protected $_tpl; 
>  protected $_error; 
>  
>  
>  public function __construct( $error = null ) { 
>  
>   $this->_error = array( 
>    'type' => self::ERR_TYPE, 
>    'description' => self::ERR_DESC 
>   ); 
The keyword "self" is always the class in which the keyword stands. Try 
this: 
 
$this->_error = array('type' => const(get_class($this) . '::ERR_TYPE'), 
'description' => const(get_class($this) . '::ERR_DESC')); 
 
>  
>   $this->_tpl = Application::Template(); 
>   $this->_tpl->assign( 'error_type', $this->_error['type'] ); 
>   $this->_tpl->assign( 'error_description', $this->_error['description'] ); 
>   $this->_msg = $error; 
>  
>   parent::__construct($this->_msg); 
>  } 
>  
>  public function Display($file, $line) { 
>   $this->_tpl->assign('admin', 1); 
>   $this->_tpl->assign('msg', $this->_msg); 
>   $file = explode('\\', $file); 
>   $top = count( $file ) - 1; 
>   $file = '/' . $file[$top-1] . '/' . $file[$top]; 
>   $this->_tpl->assign('__FILE__', $file); 
>   $this->_tpl->assign('__LINE__', $line); 
>   $this->_tpl->display('error.tpl'); 
>  } 
> } 
> ?> 
>  
> Now with this class notice the Constants i have for the class.  This class 
> is then extended by another class the ApplicationEx class 
>  
> <?php 
> /** 
> * The ApplicationEx class 
> * 
> * This is the exception class for the main Application class 
> * 
> * @author Joseph Crawford Jr. <codebowl@gmail.com> 
> * @copyright Codebowl Solutions 2005 
> * @package SimonVolkov 
> * @subpackage Exceptions 
> * 
> **/ 
> class ApplicationEx extends SvEx { 
>  
>  const ERR_TYPE = "Application Error"; 
>  const ERR_DESC = "An Application Error!"; 
>  
>  public function __construct( $msg = null ) { 
>  
>  
>   $this->_error['type'] = self::ERR_TYPE; 
>   $this->_error['description'] = self::ERR_DESC ; 
>  
>   parent::__construct($msg); 
>  
>  } 
> } 
> ?> 
>  
> Now please notice how this class has declared 2 constants for the error type 
> and description.  I know that in the first class it has constants so it 
> cannot be "changed" however with these classes i dont get an error of trying 
> to redeclare a constant that is already declared, nor does it give the 
> expected results. 
>  
> Here is a case of throwing an ApplicationEx exception 
>  
> <?php 
> if( ( isset( $dbinfo['type'] ) && $dbinfo['type'] != "" ) && ( isset( 
> $dbinfo['host'] ) && $dbinfo['host'] != "" ) && ( isset( 
> $dbinfo['database'] ) && $dbinfo['database'] != "" ) ) { 
>     self::$_db = new $dbinfo['type']($dbinfo['host'], $dbinfo['database'], 
> $dbinfo['user'], $dbinfo['pass']); 
> } else { 
>     throw new ApplicationEx("Could not instanciate the database object, you 
> must specify the database type, hostname and database name."); 
> } 
> ?> 
>  
> Now what i would expect would be for this code to set $this->_error['type'] 
> to "Application Error" since in the application class i used self::ERR_TYPE 
> and not parent::ERR_TYPE.  However it uses the constant from the SvEx class 
> and set's $this->_error['type'] = "Unknown Error" 
>  
> here is a page where you can see the results. 
>  
> http://codebowl.homelinux.net:8001/clients/JVMedia/SimonVolkov/global.php 
>  
> Any help would be appreciated. 
>  
> Joe Crawford 
> Codebowl Solutions 
> codebowl at NO SPAM gmail dot com 
>  
>  
>  
--  
------------------------------------------------------- 
Try this: SCA the Smart Class Archive for PHP 
http://www.project-sca.org 
-------------------------------------------------------
 
  
Navigation:
[Reply to this message] 
 |