开发者

php Abstracted static variables persist between static method calls

开发者 https://www.devze.com 2023-04-04 01:21 出处:网络
I have a php abstract generic class with several static variables and functions. class Generic{ public static $ID;

I have a php abstract generic class with several static variables and functions.

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
    }
}

Several classes inherit from this generic class using extends.

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);
    }
}

This way when I implement a method call on a child I can hook on the child's death. Going further, I can call:

System::GetInventory(1)

This will:

1 - Call Init() on Generic whi开发者_开发技巧ch stores "system" in the Generic::$tableName.

2 - Cenerate a nested sibling (Part) which in turn calls Init() on Generic which then moves Generic::$tableNameto Generic::$oldTableNameand stores "part in Generic::$tableName

This all works without issue. However, when I use two children back to back it falls apart.

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

Using the two back to back allows Generic to persist somehow so that Generic::$tableName == "system" when Part::GetProperty(3, "ID"); gets called.

Any idea how to fix this?


What you're looking for is late static binding which was added in PHP 5.3. Change the definition of $tableName to protected static $tableName. Then redeclare it exactly the same way in each child class.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号