静态方法调用等价于new对象吗

WBOY
Release: 2016-06-23 13:35:27
Original
1152 people have browsed it

看别人写的一个例子,似乎使用了静态调用后,就等价于new了一个对象了,不过从道理上怎么也没有理解
这到底是为什么呢


回复讨论(解决方案)

getDB里面实现了new 实例,返回的就是一个实例,所以就能用query()
静态方法是常驻内存的,调用要快一点,但是里面不能用 $this ,因为只有 new 实例以后才能用 $this

DB::getDB() 返回了一个 DB 的实例
你看看 getDB 方法的定义就清楚了

DB::getDB()
等价 
$db = new DB();
$db->getDB();

具体要看如何定义方法

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!