Difference between :: and -> in PHP

2019-03-11 03:36发布

I always see people in serious projects use :: everywhere, and -> only occasionally in local environment.

I only use -> myself and never end up in situations when I need a static value outside of a class. Am I a bad person?

As I understand, the only situation when -> won't work is when I try following:

class StaticDemo {  
    private static $static  
}

$staticDemo = new StaticDemo( );

$staticDemo->static; // wrong  
$staticDemo::static; // right

But am I missing out on some programming correctness when I don't call simple public methods by :: ?

Or is it just so that I can call a method without creating an instance?

标签: php static scope
12条回答
叼着烟拽天下
2楼-- · 2019-03-11 04:01

Use "->" when in object context and "::" when accessing the class directly. In your example that would be:

class StaticDemo {
    public static $staticVar = 'blabla';
    public $normalVar = 'foobar';
}

$foo = new StaticDemo();
echo $foo->normalVar; //object context, echoes "foobar"
echo StaticDemo::staticVar; //class or "static" context, echoes "blabla"

Read this for detailed intel.

查看更多
别忘想泡老子
3楼-- · 2019-03-11 04:02

Static methods and properties are independent of a particular instantiation of a class. These must be accessed using double colons (::). Non-static methods and properties should be accessed using ->

This allows you do to some pretty cool things. A trivial example is a counter that keeps track of the number of instances of the class exists:

class foo {
  public static $i = 0;

  public function __construct() {
    self::$i++;
  }
}

$a = new foo();
$b = new foo();
echo foo::$i;
// outputs 2
查看更多
男人必须洒脱
4楼-- · 2019-03-11 04:03

-> is for an instanciated class. :: is a static call.

:: is used in inheritance constructors (a child accessing a parent constructor) and when referring to a static method inside another method.

I wouldn't say not using static calls makes you a bad person either!

查看更多
对你真心纯属浪费
5楼-- · 2019-03-11 04:05

Or is it just so that I can call a method without creating an instance?

Correct.

The :: (scope resolution operators) are used when calling static methods/members. You don't have to create an instance to do this (like you did in your example).

Using -> and :: in the right context is the key to object-orientated programming correctness. You should only create static variables/methods when they apply to the class as a whole, and not only to a specific instance (object) of the class.

查看更多
仙女界的扛把子
6楼-- · 2019-03-11 04:07

:: is used for static methods, which you call if you have no object instance.

查看更多
仙女界的扛把子
7楼-- · 2019-03-11 04:09

Yes, you can call a method or access a value without creating an instance.

It would be useful, for example, if you have a value that all instances of a class use. Say this value, however, needs to be initialized at the beginning of your app. You could use something like StaticDemo::static = 42; to initialize it, and then all instances of your class would be able to access it.

查看更多
登录 后发表回答