What is the use of PHP’s singleton mode? What is the difference from using the class itself directly? -PHP Chinese website Q&A-What is the use of PHP’s singleton mode? What is the difference from using the class itself directly? -PHP Chinese website Q&A
What is the use of PHP’s singleton mode? What is the difference from using the class itself directly? -PHP Chinese website Q&A-What is the use of PHP’s singleton mode? What is the difference from using the class itself directly? -PHP Chinese website Q&A
Let’s take a look and learn.
单例模式可以确保这个类在全局只能有一个实例,因为它的实例是由自己保存,在类的外部也无法对该类进行实例化。而题主所说的其他方法都不容易达到这个目的。
第二个问题说php是以页面为执行单位的,我理解意思应该是以请求为执行单位的吧?每次请求过来,php解释器会解释源代码而后执行,如果需要保存这种跨请求的变量,可以使用session保存起来,对于类实例这样的变量,也可以先序列化再保存到session中,下次请求只需要反序列化就可以了。