Difference between :: and

Possible Duplicate: What's the difference between :: (double colon) and -> (arrow) in PHP?
Reference - What does this symbol mean in PHP?

I'm quite decent with PHP, but only procedural. So I decided to venture forth into learning object oriented. I'm getting the hang of it, and liking it quite well.

On PHP.net, I've always seen object oriented as mysqli::query .

However, every example I've seen uses mysqli->query as well as what I have always used.

Today, I ran across actually seeing :: used in a class example.

So now, my question is, is there a difference between :: and -> ? Is it like the difference between " and ' ?


::用于调用静态方法, ->用于实例方法


:: is the "scope resolution operator" (also aptly named Paamayim Nekudotayim), and is usually used to do a static (which means that you'll call the method in the context of the class itself, not the object) method call. There are however exceptions to this rule, such as attempting to call a parent method from an overriden method:

parent::foo(); // uses same context as when the method itself was called

It'll also allow you to reference static properties of the class, such as static properties and constants.

ClassName::FOO; 
ClassName::$property = "bar";

-> is however used to reference a property or method in the actual object instance, and will always require an object instance to the left of the operator (such as $this).

链接地址: http://www.djcxy.com/p/58012.html

上一篇: ::和。之间的区别是什么?

下一篇: ::和之间的区别