Może warto zastosować tutaj rozwiązanie z javy? Np inversion of control (injection)
Kod
[Class1]
dep=Class2,Class4
singleton=true
[Class2]
singleton=true
[Class3]
singleton=true
[Class4]
singleton=true
Kod:
<?php
function getmicrotime(){
return ((float)$usec + (float)$sec);
}
function dump($val){
/*
echo '<pre>';
var_dump($val);
echo '</pre>';
*/
}
function endTime($startTime){
return getmicrotime() - $startTime;
}
class Manager{
protected $config = NULL;
protected
$contener = array(); public function __construct(){
}
public function create($name){
if(isset($this->contener[$name])){ return $this->contener;
}
if(!isset($this->config[$name])){ throw new ClassNotExistException($name);
}
$class = new $name();
if(isset($this->config[$name]['dep'])){ $deps = explode(',', $this->config[$name]['dep']); foreach($deps as $dep){
$method = 'set'.$dep;
$class->$method($this->create($dep));
}
}
if(!empty($this->config[$name]['singleton'])){ $this->contener[$name] = $class;
}
return $class;
}
}
class Class1{
private $c2 = NULL;
private $c4 = NULL;
public function setClass2($c2){
$this->c2 = $c2;
}
public function setClass4($c4){
$this->c4 = $c4;
}
public static function GetInstance
() { if (!isset(self::$_instance)){ self::$_instance = new Class1() ;
}
return self::$_instance;
}
}
class Class2{
public static function GetInstance
() { if (!isset(self::$_instance)){ self::$_instance = new Class2() ;
}
return self::$_instance;
}
}
class Class3{
public static function GetInstance
() { if (!isset(self::$_instance)){ self::$_instance = new Class3() ;
}
return self::$_instance;
}
}
class Class4{
public static function GetInstance
() { if (!isset(self::$_instance)){ self::$_instance = new Class4() ;
}
return self::$_instance;
}
}
class ClassNotExistException extends Exception{};
$startTime = getmicrotime();
$manager = new Manager();
for ($i = 0; $i < 100; $i++) {
try{
dump($manager->create('Class2'));
dump($manager->create('Class4'));
dump($manager->create('Class1'));
}catch(ClassNotExistException $e){
dump($e->getMessage());
}
}
echo "Czas:".endTime
($startTime).' (menager)<br/>';
$startTime = getmicrotime();
for ($i = 0; $i < 100; $i++) {
$c2 = new Class2();
$c4 = new Class4();
$c1 = new Class1();
$c1->setClass2($c2);
$c1->setClass4($c4);
dump($c2);
dump($c4);
dump($c1);
}
echo "Czas:".endTime
($startTime).' (single)<br/>';
$startTime = getmicrotime();
for ($i = 0; $i < 100; $i++) {
$s2 = Class2::GetInstance();
$s4 = Class4::GetInstance();
$s1 = Class1::GetInstance();
$c1->setClass2($c2);
$c1->setClass4($c4);
dump($c2);
dump($c4);
dump($c1);
}
echo "Czas:".endTime
($startTime).' (instance)<br/>'; ?>
Co ciekawe działa podobnie szybko jak za każdym razem używać jawnie wywołania instancji, jedynie troszke dłużej trwa wczytywanie ini.
Czasy u mnie na lh:
Czas:0.0023281574249268 (menager)
Czas:0.0016329288482666 (single)
Czas:0.0024371147155762 (instance)
W javie (spring) działa to pięknie ale php z racji tworzenia od 0 za każdym wywołaniem czasami może warto poprostu nie przekombinować i tak wyszukane rozwiązania zostawić w spokoju.
Nie korzystałem z tego jeszcze w żadnym projekcie bo ostatnio nie robie wiele w php i stworzyłe tylko z ciakawości czy sie wogóle uda.
Ten post edytował Bora 5.07.2006, 00:59:55