Jump to content

Recommended Posts

Issues:

1.) Exceptions that are thrown must use a logger object from that object that threw it, without having to pass that logger object around as a parameter to every thrown exception.

 

Inheritance Chain:

 

Objects

 

// contains all abstracted logic for must objects within the system
class O_Abstract {
     $logger = NULL;
      public function getLogger() {
           return $this->logger;
      }
}

// contains all abstracted logic for any object of type "Bootstrap"
class O_Bootstrap_Abstract extends O_Abstract {
     public function registerConfigs() {
            // do something but throw exception
            throw O_Bootstrap_Exception_Abstract::getInstance()->_throw("Message",O_Manager_Bootstrap_Abstract::ERROR);
      }
}

// the object that will actually be instantiated.
class O_Manager_Bootstrap extends O_Bootstrap_Abstract {
     // in this level we will always know where to get the logger
}

 

Exceptions

// contains all abstracted logic for all Exception objects within the system
class O_Exception_Abstract extends Exception {
     public function __throw($message="", $code=0, $previous=NULL) {
               parent::__construct($message, $code, $previous);
              $this->__logLogger();
      }
      
      public function setLogger($logger="") {
            if(!empty($logger)) {
                  $this->logger = $logger;
            }
      }
      
      protected function __logLogger() {
           if(!empty($this->logger)) {
                $this->logger->log($this->message, $this->code);
           }
      }
}

// contains all abstracted logic for any object of type "Bootstrap"
class O_Bootstrap_Exception_Abstract extends O_Exception_Abstract {}

//contains all abstracted logic for any object of type "Manager_Bootstrap"
class O_Manager_Bootstrap_Exception extends O_Bootstrap_Exception_Abstract {
     public function __throw($message="", $code=0, $previous=NULL) {
               parent::__throw($message, $code, $previous);
              $this->setLogger(O_Manager_Bootstrap_Exception::getInstance()->getLogger);
      }
}

 

Example:


// init() throws O_Manager_Bootstrap_Exception
O_Manager_Bootstrap::getInstance()->init(); 
/*
this step is no big deal as the init() method resides inside the actual
O_Manager_Bootstrap class it is the lowest level class so the lowest level exception
"O_Manager_Bootstrap_Exception" can hard coded $logger = O_Manager_Bootstrap::getInstance()->getLogger();
*/

// however this call:
O_Manager_Bootstrap::getInstance()->registerConfigs();

/* resides in O_Manager_Bootstrap_Abstract object as other bootstrap type objects can use
the same functionality. Int his case O_Manager_Bootstrap will actually throw a O_Bootstrap_Exception_Abstract exception.
We can't throw a O_Manager_Bootstrap_Exception because it may how not came from a
"Manager" bootstrap, maybe it came for a "Worker" bootstrap.
*/

 

The issue is that above the intermediate (or abstraction) level we can't directly code the "object"::getInstance()->getLogger() inside the Exception because don't know what sub-type of object called it. I need to find a better way to wither code the exception into the Objects or figure out how each abstraction layer can find the child object that actually called it. As, they are going to abstract objects they will never get instantiated therefore the exception throwing must come directly from a child at some point.

 

Any suggestions?

 

 

Thanks,

 

W3Evolutions

Link to comment
https://forums.phpfreaks.com/topic/225220-abstraction-and-exception-handling/
Share on other sites

class MyException extends Exception
{
    public function __construct($message = null, $code = 0, Exception $previous = null) {
        if(Registry::isRegistered('Log')) {
            Registry::get('Log')->exception($this);
        }
        parent::__construct($message, $code, $previous);
    }
}

Registry::set('Log', new PHPLog());

try {
  throw new MyException('Oeps, I did it again!');
} catch(Exception $e) {
  echo $e->getMessage();
}

abstract class O_Abstract
{
    protected abstract function throwException($message = '', $code = 0);
}

class O_Bootstrap extends O_Abstract
{
    protected function throwException($message = '', $code = 0) {
        $this->getLogger()->log($message, $code);
        
        throw new Exception($message, $code);
    }
}

abstract class O_Abstract {
    public function getMessage() {  }
    public function getCode() {  }
}

class MyException extends Exception {
    public function __construct($caller) {
        $caller->getLogger()->log($caller->getMessage(), $caller->getCode());
        
        parent::__construct($caller->getMessage(), $caller->getCode());
    }
}

throw new MyException($this);

I planned on using a registry to store the objects in, however storing the logger object in the registry limits every object to the same log object, in turn limiting their logging to one location as apposed to each object being able to use an existing log or one it has defined. I'm working on a solution I will post soon.

 

Thanks,

 

W3Evolutions

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.