Php 单音,如果对象是新对象,则必须重置其属性 class Api\u X\u Y{ 私人$u id; 私人$_blah_头衔; 私人$XXXX; 私有静态$inputParam; 如果($inputParam){ } //存储Api_X_Y的单个实例 私有静态实例; //将对象实例化限制在类内的私有构造函数 私有函数构造($id) { echo“Test\n”; } //用于创建/返回此类的单个实例的Getter方法 公共静态函数getInstance($id) { 如果(!self::$Instance) { self::$Instance=新self($id); } 返回self::$Instance; } 公共函数resetInstance() { $this->_id=NULL; $this->_blah_title=NULL; $this->_XXXX=NULL; } } ?>

Php 单音,如果对象是新对象,则必须重置其属性 class Api\u X\u Y{ 私人$u id; 私人$_blah_头衔; 私人$XXXX; 私有静态$inputParam; 如果($inputParam){ } //存储Api_X_Y的单个实例 私有静态实例; //将对象实例化限制在类内的私有构造函数 私有函数构造($id) { echo“Test\n”; } //用于创建/返回此类的单个实例的Getter方法 公共静态函数getInstance($id) { 如果(!self::$Instance) { self::$Instance=新self($id); } 返回self::$Instance; } 公共函数resetInstance() { $this->_id=NULL; $this->_blah_title=NULL; $this->_XXXX=NULL; } } ?>,php,zend-framework,singleton,Php,Zend Framework,Singleton,好的,我想修改代码,这样当一个具有新id的新对象进入时,它将与inputParam(旧对象的id)进行比较,如果它不相同,它将重置属性(公共函数resetInstance)。 我从插入公共函数resetInstance开始,我知道我必须以某种方式实现IF语句。。。。。 有人能帮我吗?您想要一个singleton,但您希望它在ID不同时返回一个新类。单身汉在这里有什么用?看。。。系统需要它。你能给我一个主意吗?好吧,只需添加if($id!=$this->_id)resetInstance()到您的

好的,我想修改代码,这样当一个具有新id的新对象进入时,它将与inputParam(旧对象的id)进行比较,如果它不相同,它将重置属性(公共函数resetInstance)。 我从插入公共函数resetInstance开始,我知道我必须以某种方式实现IF语句。。。。。
有人能帮我吗?

您想要一个singleton,但您希望它在ID不同时返回一个新类。单身汉在这里有什么用?

看。。。系统需要它。你能给我一个主意吗?好吧,只需添加
if($id!=$this->_id)resetInstance()
到您的
getInstance($id)
方法?
class Api_X_Y {
    private $_id;
    private $_blah_title;
    private $_XXXX;
    private static $inputParam;

    if($inputParam){


    }
// Store the single instance of Api_X_Y
private static $Instance;

// Private constructor to limit object instantiation to within the class
private function __construct($id) 
{ 
    echo "Test <br />\n"; 
}

// Getter method for creating/returning the single instance of this class
public static function getInstance($id)
{
    if (!self::$Instance)
    {
        self::$Instance = new self($id);
    }

    return self::$Instance;
}


    public function resetInstance()
    {
           $this -> _id =NULL;
           $this -> _blah_title =NULL;
           $this -> _XXXX = NULL;

    }

}

?>