SingleTone,如果是新对象,则必须重置对象的属性

发布于 2024-12-05 08:50:09 字数 939 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬色的秋千 2024-12-12 08:50:10

您想要一个单例,但希望它在 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文