SingleTone,如果是新对象,则必须重置对象的属性
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;
}
}
?>
好的,我想修改代码,以便当具有新 id 的新对象进入时,它会与 inputParam(这是旧对象的 ID)进行比较,如果不相同,则会重置属性(公共函数resetInstance)。 我首先插入公共函数resetInstance,并且我知道我必须以某种方式实现 IF 语句...... 有人可以帮我吗?
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;
}
}
?>
Ok I would like to modify the code so that when a new object with a new id comes in, it gets compared with inputParam( which is the ID of the old object), and if it is not the same it gets reset of the properties (public function resetInstance).
I've started by inserting the public function resetInstance and I know that I have to implement an IF statement somehow.....
Can anyone help me out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要一个单例,但希望它在 ID 不同时返回一个新类。这里单例有什么用呢?
You want a singleton, but you want it to return a new class when an ID differs. What's the use of a singleton here?