1. Go to this page and download the library: Download kassko/class-resolver 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/ */
kassko / class-resolver example snippets
class ClientClass
{
/**
* @var string
*/
private $cacheClass;
public function __construct($cacheClass)
{
$this->cacheClass = $cacheClass;
}
public function clientFunction()
{
//...
$myCacheInstance = new $this->cacheClass;
//...
}
}
use Kassko\ClassResolver\ClassResolverInterface;
class ClientClass
{
/**
* @var string
*/
private $cacheClass;
/**
* @var ClassResolverInterface $classResolver
*/
private $classResolver;
public function __construct($cacheClass, ClassResolverInterface $classResolver)
{
$this->cacheClass = $cacheClass;
$this->classResolver = $classResolver;
}
public function clientFunction()
{
//...
$myCacheInstance = $this->classResolver->resolve($this->cacheClass);
//...
}
}
use Kassko\ClassResolver\ClassResolverInterface;
class ClientClass
{
/**
* @var string
*/
private $cacheClass;
/**
* @var ClassResolverInterface $classResolver
*/
private $classResolver;
public function __construct($cacheClass, ClassResolverInterface $classResolver)
{
$this->cacheClass = $cacheClass;
$this->classResolver = $classResolver;
}
public function clientFunction()
{
//...
if ($this->classResolver->support($this->cacheClass)) {
$myCacheInstance = $this->classResolver->resolve($this->cacheClass);
} else {
//Here, the code that handle the case when the class cannot be resolved.
}
//...
}
}
use Kassko\ClassResolver\MapClassResolver;
$mapClassResolver = new MapClassResolver(['Kassko\Sample\CacheClass' => $cacheInstance]);
use Kassko\ClassResolver\MapClassResolver;
$mapClassResolver = new MapClassResolver(['Kassko\Sample\CacheClass' => function () use ($cacheInstance) {return new Kassko\Sample\CacheClass(new Kassko\Sample\Dependency);};
$classResolver = new VariableContainerAwareClassResolver($container, 'get', 'has');
$container = new MyContainerAdapter($container);
$classResolver = new ContainerAwareClassResolver($container);
$container = new MyContainerAdapter($container);
$cacheInstance = new Kassko\Sample\CacheClass($someDependency);
$classResolver = new ClosureClassResolver(function ($className) {
switch ($className) use ($cacheInstance) {
case 'Kassko\Sample\CacheClass':
return $cacheInstance;
}
});
class CacheFactory
{
public function supports($className) {return 'Kassko\Sample\CacheClass' === $className;}
public function get($className) {return new $className;}
}
$factory = new FactoryAdapterClassResolver();
$classResolver = new ContainerAwareClassResolver($factory, 'supports', 'get');
class CacheFactory
{
public static function supports($className) {return 'Kassko\Sample\CacheClass' === $className;}
public static function get($className) {return new $className;}
}
$classResolver = new ContainerAwareClassResolver('StaticFactoryAdapterClassResolver', 'supports', 'get');
$classResolver = new ClassResolverChain();
$classResolver->add($mapClassResolver);
$classResolver->add($closureClassResolver);
use Kassko\ClassResolver\DefaultClassResolver;
$classResolver = new ClassResolverChain();
$classResolver->add($mapClassResolver);
$classResolver->add($closureClassResolver);
$classResolver->setDefaultResolver(new DefaultClassResolver(['throw_exception']));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.