\PhalApi_DI

Dependence Injection Class

Dependency Injection Container

  • support: setter/getter, magic method like setX/getX, class property like $di->X, array like $di['X']
  • ways to initialize: assign directly, or by class name, or by anonymous function


Usage:

      $di = new PhalApi_DI();

      // setter/getter, magic method like setX/getX, class property like $di->X, array like $di['X']
      $di->key = 'value';
      $di['key'] = 'value';
      $di->set('key', 'value');
      $di->setKey('value');

      echo $di->key;
      echo $di['key'];
      echo $di->get('key');
      echo $di->getKey();

      // ways to initialize: assign directly, or by class name(will call onInitialize method), or by anonymous function
      $di->simpleKey = array('value');
      $di->classKey = 'PhalApi_DI';
      $di->closureKey = function () {
           return 'sth heavy ...';
      };

Default Services:

Summary

Methods
Properties
Constants
__construct()
one()
onConstruct()
onInitialize()
set()
get()
__call()
__set()
__get()
offsetSet()
offsetGet()
offsetUnset()
offsetExists()
$request
$response
$cache
$crypt
$config
$logger
$notorm
$loader
$tracer
No constants found
initService()
resetHit()
isFirstHit()
recordHitTimes()
$instance
$hitTimes
$data
N/A
No private methods found
No private properties found
N/A

Properties

$request

$request : \PhalApi_Request

request

Type

\PhalApi_Request

$response

$response : \PhalApi_Response_Json

reponse

Type

\PhalApi_Response_Json

$cache

$cache : \PhalApi_Cache

cache

Type

\PhalApi_Cache

$crypt

$crypt : \PhalApi_Crypt

crypt

Type

\PhalApi_Crypt

$config

$config : \PhalApi_Config

config

Type

\PhalApi_Config

$logger

$logger : \PhalApi_Logger

logger

Type

\PhalApi_Logger

$notorm

$notorm : \PhalApi_DB_NotORM

NotORM

Type

\PhalApi_DB_NotORM

$loader

$loader : \PhalApi_Loader

loader

Type

\PhalApi_Loader

$tracer

$tracer : \PhalApi_Helper_Tracer

tracer

Type

\PhalApi_Helper_Tracer

$instance

$instance : \PhalApi_DI

Type

\PhalApi_DI — singleton object

$hitTimes

$hitTimes : array

Type

array — services hit times

$data

$data : array

Type

array — services regsitration pool

Methods

__construct()

__construct() 

one()

one() 

Get the singletom of DI

    1. construct and initialize default services such as reqeust, response
    1. you can also create by new, but can not share servcies

onConstruct()

onConstruct() 

Construct on services

    1. custom specified operations on your logic business, e.g. add default services
    1. only will triggle at first time

onInitialize()

onInitialize() 

set()

set(string  $key, mixed  $value) 

Setter

    1. save meta constructor, and initilize when in need (lazy initialization)

Parameters

string $key

service name, unique, case sensitive

mixed $value

service value, it could by some value, instance, class name, or anonymous function

get()

get(string  $key, mixed  $default = NULL) : mixed

Getter

    1. get the value of service, and initiliaze for different cases
    1. try to call onConstruct() when first time to create

Parameters

string $key

service name, unique, case sensitive

mixed $default

service default value

Returns

mixed —

return NULL when service not exists

__call()

__call(  $name,   $arguments) 

------------------ Magic Methods ------------------ *

Parameters

$name
$arguments

__set()

__set(  $name,   $value) 

Parameters

$name
$value

__get()

__get(  $name) 

Parameters

$name

offsetSet()

offsetSet(  $offset,   $value) 

------------------ ArrayAccess Interfaces ------------------ *

Parameters

$offset
$value

offsetGet()

offsetGet(  $offset) 

Parameters

$offset

offsetUnset()

offsetUnset(  $offset) 

Parameters

$offset

offsetExists()

offsetExists(  $offset) 

Parameters

$offset

initService()

initService(  $config) 

------------------ Potected Methods ------------------ *

Parameters

$config

resetHit()

resetHit(  $key) 

Parameters

$key

isFirstHit()

isFirstHit(  $key) 

Parameters

$key

recordHitTimes()

recordHitTimes(  $key) 

Parameters

$key