PHP - Calling a class function from another class -


i trying create error handling class can called other classes, when call function within error handling class class, following error:

fatal error: call member function fetch_error_text() on null in (filepath_here) on line 361

here code have far:

global $errhandle_func_call;     class err_handle {    public function __construct()    {    }     public function fetch_error_text($err_text)    {        $err_text_display = $return_msg[$err_text];        return "<h4>" . $err_text_display . "</h4>";    } }  $errhandle_func_call = new err_handle();  class basket {     public function view_basket()     {         //some_code_here         if($query->num_rows < 1)         {             echo $errhandle_func_call->fetch_error_text("basket_empty");         }     } } 

thanks.

i not recommend using globals...but should fix it...global needs in every function uses it. looks need global $return_msg.

global $errhandle_func_call;     class err_handle {    public function __construct()    {    }     public function fetch_error_text($err_text)    {        global $return_msg;        $err_text_display = $return_msg[$err_text];        return "<h4>" . $err_text_display . "</h4>";    } }  $errhandle_func_call = new err_handle();  class basket {     public function view_basket()     {         global $errhandle_func_call;         //some_code_here         if($query->num_rows < 1)         {             echo $errhandle_func_call->fetch_error_text("basket_empty");         }     } } 

[update 2015-04-29] heck of it, quick, crude, introduction recommendations...from easiest hardest...i'm going change casing all_uppercase used denote constants , i'm bit ocd.

static error class:

class errortype {     const usererror     = 1;     const notfounderror = 2;      public static function getmessage( $messageid ) {         switch( $messageid ) {             case self::usererror: return "user error";             case self::notfounderror: return "not found error";         }     } } class errorhandler {     public static function fetcherrortext( $errortype) {         return "<h4>".errortype::getmessage($errortype)."</h4>";     } }  errorhandler::fetcherrortext( errortype::usererror ); 

this easiest , gets away globals. added errortype class reduce "magic strings" in code giving constant values pass function. avoid typos, etc., ide can give intellisense it.

however, static classes not friendly unit tests. so, that's "inversion of dependency" can come play. easiest way invert dependencies service locator because don't have able modify constructor able pass in instance of object. consider anti-pattern, it's extremely useful in right situations (e.g. convention on configuration, etc.).

inversion of dependency: service locator

//first thing need inversion interface interface ihandleerror {     public function geterrormessage( $errortype ); }  //imaginary servicelocator class, defining instance of interface use servicelocator::define( array(     'ihandleerror' => 'somenamespace\errorhandler' ) );  class basket {     public function viewbasket() {         //grab when need         $errorhandler = servicelocator::get('ihandleerror');         if( query->num_rows < 1 ) {             echo $errorhandler->geterrormessage( errortype::basketempty );         }     } } 

the servicelocator object imaginary...but in simplest form it's array of key => value, value points class...and ::get() method instantiates instance or singleton of class. i'm doing more "magic strings" here, didn't want make convoluted.

inversion of dependency: dependency injection

dependency injection, on other hand, simpler servicelocator in concept...but harder in implementation because need access modify constructor of class , able modify instantiations pass in object.

class basket {     private $_errorhandler;     public function __construct( ihandleerror $errorhandler ) {         $this->_errorhandler = $errorhandler;     } }  $basket = new basket( $errorhandler ); 

any of these 3 steer away globals , improve maintainability of code bit.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -