Is there a way to define an object as null from within the object?

时间:2018-03-09 19:21:33

标签: php code-design

I'm currently doing a controller, and I want the controller to be null if the parameter pass to the controller does not match an entry in the database, so my question is the following, can I set the object instance to null from the object constructor?

Here's what I'm trying to achieve

public function __construct() {
    unset($this);
}

Is this possible, or just a bad design and why?

1 个答案:

答案 0 :(得分:2)

这实际上是不可能的,考虑到你必须以某种方式清除对象本身内对象的所有外部引用,这是不可能的。

此外,这是糟糕的设计,因为控制器对象应该是模型和视图之间的中介,并且本身不应该依赖于模型值。

它是一个表示数据库实体的模型对象,因为它在数据库中不存在,所以它将为null。