Contao\System::importStatic PHP Méthode

importStatic() public static méthode

Import a library in non-object context
public static importStatic ( string $strClass, string $strKey = null, boolean $blnForce = false ) : object
$strClass string The class name
$strKey string An optional key to store the object under
$blnForce boolean If true, existing objects will be overridden
Résultat object The imported object
    public static function importStatic($strClass, $strKey = null, $blnForce = false)
    {
        $strKey = $strKey ?: $strClass;
        if ($blnForce || !isset(static::$arrStaticObjects[$strKey])) {
            $container = static::getContainer();
            if (!class_exists($strClass) && $container->has($strClass)) {
                static::$arrStaticObjects[$strKey] = $container->get($strClass);
            } elseif (in_array('getInstance', get_class_methods($strClass))) {
                static::$arrStaticObjects[$strKey] = call_user_func(array($strClass, 'getInstance'));
            } else {
                static::$arrStaticObjects[$strKey] = new $strClass();
            }
        }
        return static::$arrStaticObjects[$strKey];
    }

Usage Example

 /**
  * @param \Closure|array $callback
  * @param array $args
  *
  * @return mixed
  */
 private function executeCallback($callback, array $args)
 {
     // Support Contao's getInstance() method when callback is an array
     if (is_array($callback)) {
         return call_user_func_array([System::importStatic($callback[0]), $callback[1]], $args);
     }
     return call_user_func_array($callback, $args);
 }
All Usage Examples Of Contao\System::importStatic