Application configuration

Config a genereal configuration manager

A Config object can be used to store any type of PHP variables. An application can have multiple named singleton configuration objects. These objects are accessible via a static Config::instance() method.

  • Canfiguration manager example
    1. <?php
    2.  
    3. namespace Sphp\Config;
    4.  
    5. $conf1 = Config::instance("full of foo");
    6. $conf1->set("foo", "bar");
    7. $conf1->foobar = 'baz';
    8. Config::instance("full of foo")["bar"] = 10.2;
    9.  
    10. echo "variable 'foo' in 'full of foo' is: $conf1->foo \n";
    11. echo "variable 'foobar' in 'full of foo' is: {$conf1["foobar"]} \n";
    12.  
    13. print_r(Config::instance("full of foo")->toArray());
    14.  
    Highlighted with GeSHi 1.0.9.1
  • Execution result as highlighted code
    1. variable 'foo' in 'full of foo' is: bar
    2. variable 'foobar' in 'full of foo' is: baz
    3. Array
    4. (
    5.     [foo] => bar
    6.     [foobar] => baz
    7.     [bar] => 10.2
    8. )
    9.  
    Highlighted with GeSHi 1.0.9.1

PHP is a runtime PHP environment manager container

PHP utility class handles common PHP configuration related tasks via build-in singleton PHPConfig and named PHPIni instances.

PHPConfig is a runtime PHP environment manager

PHPConfig object can handle basic runtime PHP environment configuration. Objects configuration settings can be reinitialized during script execution and thus a programmer can change the behaviour of the PHP environment by simply using multiple instances of PHPConfig.

  • PHP environment manipulation example
    1. <?php
    2.  
    3. namespace Sphp\Config;
    4.  
    5. PHP::Config()->setDefaultTimezone("Pacific/Honolulu");
    6. echo "Current time in Honolulu Hawaii:\t" . date('H:i:s T') . "\n";
    7.  
    8. PHP::Config()->setDefaultTimezone("Europe/Helsinki");
    9. echo "Current time in Helsinki Finland:\t" . date('H:i:s T');
    10.  
    Highlighted with GeSHi 1.0.9.1
  • Execution result as highlighted code
    1. Current time in Honolulu Hawaii:        04:10:57 HST
    2. Current time in Helsinki Finland:       17:10:57 EEST
    Highlighted with GeSHi 1.0.9.1

PHPIni is a runtime PHP ini manager

PHPIni object contains a set of configuration options (see ini_set() in PHP manual). This set of options can be set and reset at any time during script execution.

Important: Not all the available options can be changed using PHPIni. There is a list of all available options in the ini.list.php.

  • Runtime PHP INI manipulation example
    1. <?php
    2.  
    3. namespace Sphp\Config;
    4.  
    5. $fail = function () {
    6.   echo "<pre>";
    7.   echo 'display_errors: ' . ini_get('display_errors') . "\n";
    8.   echo "error_reporting: " . ini_get('error_reporting') . '</pre>';
    9. };
    10.  
    11. PHP::ini('show_errors')
    12.         ->set('display_errors', 1)
    13.         ->set('error_reporting', E_ALL)
    14.         ->execute($fail);
    15.  
    16. PHP::ini('hide_errors')
    17.         ->set('display_errors', 0)
    18.         ->set('error_reporting', 0)
    19.         ->init();
    20. $fail();
    21.  
    Highlighted with GeSHi 1.0.9.1
  • Execution result as HTML5 flow
    display_errors: 1
    error_reporting: 32767
    display_errors: 0
    error_reporting: 0

PHP 7 Error and Exception handling

In PHP 7.2+ most errors are reported by throwing Error exceptions. These can be catched by using language.exceptions.php blocks. If there are no matching blocks, then any default exception handler installed with set_exception_handler() will be called, and if there is no default exception handler, then the exception will be converted to a fatal error and will be handled like a traditional error.

ErrorDispatcher object to manage PHP error and exception listeners

This class can manage both PHP errors and uncaught Throwable objects.

ErrorDispatcher as PHP Error manager

An ErrorDispatcher object replaces PHP's native error handler and sends PHP errors to its error listeners. An Error listener must be a callable or of ErrorListener type.

All of the following PHP error types cannot be handled by an ErrorDispatcher instance

  • PHP code
    1. <?php
    2.  
    3. namespace Sphp\Config\ErrorHandling;
    4.  
    5. use Sphp\Html\Foundation\Sites\Core\ErrorCalloutBuilder;
    6.  
    7. $ed = new ErrorDispatcher();
    8. $ed->addErrorListener(\E_ALL, new ErrorCalloutBuilder(true, true), 3);
    9. $ed->startErrorHandling();
    10. include_once('foobar');
    11. trigger_error('User defined Errors suck badly', E_USER_ERROR);
    12. trigger_error('User warnings suck', E_USER_WARNING);
    13. trigger_error('Deprecated user features suck', E_USER_DEPRECATED);
    14. trigger_error('User defined Notes suck a bit', E_USER_NOTICE);
    15.  
    16. echo $foo;
    17.  
    18.  
    Highlighted with GeSHi 1.0.9.1
  • Execution result as HTML5 flow
    E_WARNING
    include_once(foobar): failed to open stream: No such file or directory
    on line 10 of file:
    /home/int48291/public_html/playground/manual/examples/Sphp/Config/ErrorHandling/ErrorDispatcher.php
    E_WARNING
    include_once(): Failed opening 'foobar' for inclusion (include_path='.:/opt/cpanel/ea-php73/root/usr/share/pear:/home/int48291/public_html/playground/sphp:/home/int48291/public_html/playground/manual/examples:/home/int48291/public_html/playground/manual/pages:/home/int48291/public_html/playground:/home/int48291/public_html/playground/manual')
    on line 10 of file:
    /home/int48291/public_html/playground/manual/examples/Sphp/Config/ErrorHandling/ErrorDispatcher.php
    E_USER_ERROR
    User defined Errors suck badly
    on line 11 of file:
    /home/int48291/public_html/playground/manual/examples/Sphp/Config/ErrorHandling/ErrorDispatcher.php
    E_USER_WARNING
    User warnings suck
    on line 12 of file:
    /home/int48291/public_html/playground/manual/examples/Sphp/Config/ErrorHandling/ErrorDispatcher.php
    E_USER_DEPRECATED
    Deprecated user features suck
    on line 13 of file:
    /home/int48291/public_html/playground/manual/examples/Sphp/Config/ErrorHandling/ErrorDispatcher.php
    E_USER_NOTICE
    User defined Notes suck a bit
    on line 14 of file:
    /home/int48291/public_html/playground/manual/examples/Sphp/Config/ErrorHandling/ErrorDispatcher.php
    E_NOTICE
    Undefined variable: foo
    on line 16 of file:
    /home/int48291/public_html/playground/manual/examples/Sphp/Config/ErrorHandling/ErrorDispatcher.php

ErrorDispatcher as a Uncaught Throwable handler

In PHP 7, most errors are reported by throwing Error exceptions. Both Error and Exception implements the Throwable interface.

An Exception listener must be a callable or of ExceptionListener type.

NOTE: It is important to note that Script execution will stop after a handler is called.

  • An example of Error and Exception handling
    1. <?php
    2.  
    3. namespace Sphp\Config\ErrorHandling;
    4.  
    5. use Sphp\Html\Foundation\Sites\Core\ErrorCalloutBuilder;
    6. use Sphp\Html\Foundation\Sites\Core\ThrowableCalloutBuilder;
    7.  
    8. $ed = new ErrorDispatcher();
    9. $ed->addErrorListener(\E_ALL, new ErrorCalloutBuilder(true, true), 1);
    10. $ed->startErrorHandling();
    11.  
    12. $ed->addExceptionListener(new ExceptionLogger('logs/exceptions.log'));
    13. $ed->addExceptionListener((new ThrowableCalloutBuilder())->showInitialFile()->showTrace()->showPreviousException());
    14. $ed->startExceptionHandling();
    15.  
    Highlighted with GeSHi 1.0.9.1

ErrorToExceptionThrower for multiple error handlers

ErrorToExceptionThrower can temporarily convert PHP errors or warnings to any type of Exception objects and then re-set the PHP error handler as it was.

  • PHP code
    1. <?php
    2.  
    3. namespace Sphp\Config\ErrorHandling;
    4.  
    5. use Throwable;
    6. use Sphp\Html\Foundation\Sites\Core\ThrowableCalloutBuilder;
    7.  
    8. $thrower = ErrorToExceptionThrower::getInstance();
    9. $thrower->start();
    10. try {
    11.   include("missing/file.php");
    12. } catch (Throwable $ex) {
    13.   echo ThrowableCalloutBuilder::build($ex);
    14. }
    15. $thrower->stop();
    16.  
    17. ErrorToExceptionThrower::getInstance(\Exception::class)->start();
    18. try {
    19.   include("missing/file.php");
    20. } catch (Throwable $ex) {
    21.   echo ThrowableCalloutBuilder::build($ex);
    22. }
    23. ErrorToExceptionThrower::getInstance(\Exception::class)->stop();
    24.  
    Highlighted with GeSHi 1.0.9.1
  • Execution result as HTML5 flow

    ErrorException

    • Message: include(missing/file.php): failed to open stream: No such file or directory

    Exception

    • Message: include(missing/file.php): failed to open stream: No such file or directory