php konteyner sınıfı: neden herkes daha karmaşık bir yöntem kullanır?

0 Cevap php

Ben php için komut dosyaları bulmak veya php çerçeveler baktığımızda sık sık kullanan değişkenleri veya diğer nesneleri tutan bir "kayıt defteri sınıf" veya bir "kapsayıcı sınıfı" bakın __ sihirli yöntemini olsun.

Burada ne demek istediğimi bir basitleştirilmiş bir örnektir:

example 1:

class container {
 private $objects;
 public function __get($class){
  if(isset($this->objects[$class])){
   return $this->objects[$class];
  }
  return $this->objects[$class] = new $class();
 }
}

the above example will have more functions to it when creating the class instead of just calling it but for my example it should be enough. "example 1" is how I mostly see it in scripts downloaded from the internet, it maintains a single class instance, now what I'm wondering is that wouldn't this example do the same thing and be more efficient:

example 2:

class simplecontainer {
 public function __get($class){
  return $this->$class = new $class();
 }
}

Ama beni bile kullanmaya düşünmeden önce iki kere düşünmek kılan diğer halklar komut dosyalarında "örnek 2" görmek asla.

Ben benim yerel makinede birkaç onlar içerecek sınıfları ve 100000 kez etrafında yeniden kullanımını ve "örnek 1" 0.75seconds içinde bulunmamış kullanarak simplecontainer vs konteyner test edilmiş ve "örnek 2" 0.29seconds bunu yapar.

Paramı hangi komut dosyalarında kullanmak gerekir? örnek 1 veya örnek 2? ve neden?

0 Cevap