farklı bir tanımlayıcı ile nesne başına php tekiz

1 Cevap php

Bu benim geçen gün düşünüyordum şeydir.

Ben bir tek benzeri bir nesne istiyorum. Bunun yerine bir sınıfın tek bir örneği, ben eşleşen değişkeni ile tek bir nesne istiyorum.

Örneğin.

an existing employee object has a employee_id = 100 the getEmployee static method is called with employee_id = 100, i want to return the already existing object with the matching employee_id or create it if it does not exist.

Bu yapılabilir?

Teşekkürler

1 Cevap

Tekil bir dizi ile bir tek, gibi mi? Ben sizin sınıfta, belki buna benzer bir şey yapılabilir eminim:

 public static function getInstance($id) {                                                               
     if (self::$_instances[$id] == null) {
        self::$_instances[$id] = new self;
     }
     return self::$_instance[$id];
 }

Aslında, vb öğe almak için, ve bunu test etmedi tabii ki değişiklik gerekir, ama sadece bir düşünce ...