PHP code example of phossa / phossa-shared

1. Go to this page and download the library: Download phossa/phossa-shared 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/ */

    

phossa / phossa-shared example snippets


  
  namespace Phossa\Cache\Exception;

  class BadMethodCallException extends
      \Phossa\Shared\Exception\BadMethodCallException
  {
  }

  

    
    namespace Phossa\Cache\Message;

    use Phossa\Shared\Message\MessageAbstract;

    class Message extends MessageAbstract
    {
        // use current year_month_date_hour_minute
        const CACHE_MESSAGE     = 1512220901;

        // driver failed
        const CACHE_DRIVER_FAIL = 1512220902;

        protected static $messages = [
            self::CACHE_MESSAGE      => 'cache %s',
            self::CACHE_DRIVER_FAILT => 'cache driver %s failed',
        ];
    }
    

    use Phossa\Cache\Message\Message;
    ...
    // throw exception
    throw new Exception\RuntimeException(
        Message::get(Message::CACHE_DRIVER_FAIL, get_class($driver)),
        Message::CACHE_DRIVER_FAIL
    );
    

    namespace Phossa\Cache;

    use Phossa\Shared\Message\Loader\LanguageLoader;

    // set language to 'zh_CN'
    $langLoader = new LanguageLoader('zh_CN');

    // will load local `Message\Message.zh_CN.php` language file
    Message\Message::setLoader($langLoader);

    // print message in chinese
    echo Message\Message::get(
        Message::CACHE_MESSAGE, get_class($object)
    );
    

    namespace Phossa\Cache;

    use Phossa\Shared\Message\Formatter\HtmlFormatter;

    // format message as HTML
    $formatter = new HtmlFormatter();

    Message\Message::setFormatter($formatter);

    // print as HTML
    echo Message\Message::get(
        Message::CACHE_MESSAGE, get_class($object)
    );
    

    
    namespace Phossa\MyPackage;

    class MyStaticClass extends SomeClass
    {
        use \Phossa\Shared\Pattern\StaticTrait;
        ...
    }
    

    
    namespace Phossa\MyPackage;

    class MyStaticClass extends \Phossa\Shared\Pattern\StaticAbstract
    {
        ...
    }
    

    
    namespace Phossa\MyPackage;

    class MySingletonClass extends SomeClass
    {
        use \Phossa\Shared\Pattern\SingletonTrait;
        ...
    }
    

    $obj =  MySingletonClass::getInstance();
    

    
    namespace Phossa\MyPackage;

    class MyNewSingletonClass extends MySingletonClass
    {
        ...
    }
    

    
    namespace Phossa\Event;

    use Phossa\Shared\Pattern\ShareableInterface;

    class EventManager implements ShareableInterface
    {
        use \Phossa\Shared\Pattern\ShareableTrait;
        ...
    }
    

    // global event manager instance
    $globalEM = EventManager::getShareable();

    // local event manager
    $localEM  = new EventManager();

    // this EM global ?
    if ($globalEM->isShareable()) {
        ...
    } else {
        ...
    }