1. Go to this page and download the library: Download seotils/deferred-exceptions library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
seotils / deferred-exceptions example snippets
namespace My;
use Seotils\Traits\DeferredExceptions;
/**
* Exception class to clearly understand
* the class of the error occurred (will be autodetected)
*/
class SomeClassException extends \Exception {
}
/**
* Custom exception class (must be to passed as last argument to the ::exception() function)
*/
class MyCustomException extends \Exception {
}
class SomeClass {
use DeferredExceptions;
public function troubles(){
try {
// A problem code ..
} catch( Exception $exc ) {
// Old behavior:
// throw new SomeClassException('Oh no!', 0, $exc);
// Short syntax. Exception class will be detected as `My\SomeClassException`
$this->exception('Oh no!');
// Full syntax
$this->exception(
'Oh no!', // Error message
0, // Error code
// Previous exception.
// Used if only exception are thrown immediately with ::useExceptions( true )
$exc, // Default: null
// Custom exception class name
'My\MyCustomException'
);
}
}
public function anotherTroubles(){
// You can clear exception stack
// NOTE. ::releaseExceptions() do not clean global stack of an `DeferredException` class !!!
// You must to use ::releaseGlobalExceptions()
$this->releaseExceptions();
try {
// A problem code ..
} catch( Exception $exc ) {
...
}
}
public function andTroubles(){
// You can detect if an instance of the class has deffered exceptions
if( $this->hasExceptions() ){
// Do somthing. For example assign default properties.
}
try {
// A problem code ..
} catch( Exception $exc ) {
...
}
}
}
...
foreach( $list as $item){
/* @var $item \My\SomeClass */
// Don't throws an exceptions.
$item->useExceptions( false );
...
// Calls a "bad" functions
$item->troubles();
$item->anotherTroubles();
$item->andTroubles();
...
// throws last exception was occured
$item->throwLast(
true // Release last exception in stack after throw
);
// throws an exception with a list of all exception was occured
// using custom template of each item in the list
$item->throwAll(
true, // Release exception stack after throw
// Template
'`::class` exception was thrown at ::microtime with code #::code and say "::message"'
);
// Get an array of all exceptions of this class instance
$errors = $item->getExceptions();
foreach( $errors as $error)
{
$error ['time']; // Microtime of an exception
$error ['class']; // Class of an exception
$error ['code']; // Error code
$error ['message']; // Error message
}
}
...
// If some exeptions was occured
if( DeferredExceptions::hasGlobalExceptions() ) {
// Throws exception with a list of all exceptions of all derived classes.
DeferredExceptions::throwGlobal(
true, // Release exception stack after throw
// Template
'`::class` exception was thrown at ::microtime with code #::code and say "::message"')
);
// Or just get a list of exceptions for manual processing
$errors = DeferredExceptions::getGlobalExceptions();
foreach( $errors as $error)
{
$error ['time']; // Microtime of an exception
$error ['class']; // Class of an exception
$error ['code']; // Error code
$error ['message']; // Error message
}
}
// Now we can clear the global exception stack and try another sequence
// NOTE. ::releaseGlobalExceptions() do not clear exceptions stacks of instances of a `DeferredException` class !!!
// You must to use ::releaseExceptions() for each instance if you are want to use these
// class instanses in second time with a clean exceptions history.
DeferredExceptions::releaseGlobalExceptions();
foreach( $secondList as $item){
/* @var $item \My\SomeClass */
// Don't throws an exceptions.
$item->useExceptions( false );
// If $item was used in the previous sequence,
// then clear the exceptions stack of an instance
if( $item->hasExceptions()){
$item->releaseExceptions();
}
...
// Calls a "bad" functions
$item->troubles();
$item->anotherTroubles();
$item->andTroubles();
...
}
...
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.