2011-09-09 5 views
0

저는 몇 가지 정적 변수와 함수가있는 PHP 추상 제네릭 클래스를 사용합니다.php 추상화 된 정적 변수는 정적 메서드 호출간에 지속됩니다.

class Generic{ 
    public static $ID; 
    private static $tableName; 
    private static $oldTableName; 
    protected static $DAL; 

    public static function Init($tableName, $id=null) { 

     if(self::$tableName) 
      self::$oldTableName=self::$tableName; 

     self::$tableName=$tableName;   

     self::$DAL = new DAL(self::$tableName); 
    } 

    public static function RecallTableName(){ 
     if(self::$oldTableName){ 
      self::$tableName=self::$oldTableName; 
      self::$oldTableName=null; 
      return true; 
     } 
    } 

    public static function GetProperty($id, $columnName, $whereStatement = "1=1"){ 
     if(!self::$tableName) 
      return false; 

     //DO DB STUFF HERE 

     return; //RETURN WHAT DB CALLS DID HERE 
    } 
} 

여러 클래스를 확장하여이 제네릭 클래스에서 상속합니다.

class Part extends Generic { 

    static function Init($tableName="part",$id = null) { 
     $return = parent::Init($tableName,$id); 
     new Part(); 
     return $return; 
    } 

    public static function destruct(){ 
     parent::RecallTableName(); 
    } 

    public function __destruct() { 
     Part::destruct(); 
    } 

    public static function GetProperty($id, $columnName, $whereStatement = "1=1") { 
     self::Init(); 
     return parent::GetProperty($id, $columnName, $whereStatement); 
    } 
} 

class System extends Generic { 

    static function Init($tableName="system",$id = null) { 
     $return = parent::Init($tableName,$id); 
     new System(); 
     return $return; 
    } 

    public static function destruct(){ 
     parent::RecallTableName(); 
    } 

    public function __destruct() { 
     Part::destruct(); 
    } 

    public static function GetProperty($id, $columnName, $whereStatement = "1=1") { 
     self::Init(); 
     return parent::GetProperty($id, $columnName, $whereStatement); 
    } 

    public static function GetInventory($PartManageIDToCheck) 
    { 
     return Part::GetInventory($PartManageIDToCheck); 
    } 
} 

이렇게하면 자식에 대한 메서드 호출을 구현할 때 자식의 죽음을 연결할 수 있습니다. 더 나아가, 나는 호출 할 수

System::GetInventory(1) 

이됩니다
이 - 1 Generic::$tableNameGeneric에 저장 "시스템"Init()를 호출합니다.
2 - 중첩 된 형제 차례로 다음 문제없이 Generic::$tableName

이 모든 작품 Generic::$tableNameGeneric::$oldTableName에 저장 "부분을 이동하는 Init()Generic에 호출 (파트) Cenerate 그러나, 나는 다시 다시 두 아이를 사용하는 경우. 그것은 떨어져 떨어진다.

System::GetProperty(1, "ID"); 
Part::GetProperty(3, "ID"); 

Generic::$tableName == "system" 것을 Part::GetProperty(3, "ID");가 호출 될 때, 그래서 다시 다시 두 사람은 어떻게 든 지속 일반 수 있습니다 사용.

이 문제를 해결하는 방법에 대해 알고 싶습니다.

답변

1

당신이 찾고있는 것은 PHP 5.3에 추가 된 late static binding입니다. $ tableName의 정의를 protected static $tableName으로 변경하십시오. 그런 다음 각 하위 클래스에서 정확히 동일한 방식으로 다시 선언하십시오.