What's the difference between :: (double colon) and -> (arrow) in PHP?

What's the difference between :: (double colon) and -> (arrow) in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

What's the difference between :: (double colon) and -> (arrow) in PHP?

In PHP, you encounter two distinct ways to access methods or properties: the object operator (->) and the scope resolution operator (::). While beginners often assume a simple mapping—object for variables, class for static methods—the reality is slightly more nuanced. Understanding exactly when and why to use each prevents subtle bugs and clarifies how PHP handles namespaces and inheritance.

Object Operator (->)

The -> operator is the standard mechanism for accessing properties and methods of an instance (an object). When you write code like $response->setParameter('foo', 'bar'), you are interacting with a specific instance created from a class. This operator binds directly to the memory address of that specific object.

class Config {
    public $debug = false;
    
    public function setParam($key, $value) {
        $this->settings[$key] = $value;
    }
}

$config = new Config();
$config->setParam('foo', 'bar'); // Works: accessing instance state

You can also use -> to access properties that are not methods but hold data. For example, $object->name retrieves the value of the name property on that specific object instance.

Scope Resolution Operator (::)

The :: operator is primarily used to access static members of a class. Static members belong to the class definition itself, not to any specific instance. This means they exist even if you never create an object using new.

sfConfig::set('foo', 'bar'); // Works: accessing static state

In the example above, sfConfig is a class name, and set is likely a static method. Calling this does not require instantiating sfConfig; it operates on the class namespace directly. This is common for utility functions, configuration stores, or factory methods that don't maintain internal instance state.

Static vs. Instance: A Critical Distinction

The core difference lies in scope and memory. If you try to access a non-static property using ::, PHP will throw a fatal error because static members cannot see the $this context required for instance properties. Conversely, trying to call a static method on an instance (e.g., $instance::method()) is syntactically valid but semantically confusing and often discouraged unless you are explicitly calling a static method defined within the class.

class Example {
    public $publicInstanceProperty = 'value';
    
    public static function staticMethod() {
        echo "Static called";
    }
}

// Correct: Accessing instance property via instance
$obj = new Example();
echo $obj->publicInstanceProperty; 

// Incorrect: Trying to access instance property statically
// echo Example::publicInstanceProperty; // Fatal Error

// Correct: Accessing static method via class or instance (though class is preferred)
Example::staticMethod(); 
$obj::staticMethod(); // Also works, but less clear

The = vs. => Operators

Your question regarding assignment operators highlights another common point of confusion. The = operator is the standard assignment operator used to assign values to variables or properties on instances.

$x = 10;
$obj->count = 5;

The => operator, however, is specifically designed for arrays. It associates a key with a value within an array structure. You cannot use => for assigning values outside of arrays or for setting properties on objects directly.

// Correct: Associative array definition
$assoc = ['name' => 'John', 'age' => 30];

// Incorrect: Using arrow for object property assignment
$obj->name => 'Jane'; // Fatal Error

When to Use Which?

The decision is almost always dictated by whether the data or behavior belongs to a specific instance or the class as a whole.

  • Use -> when you need to modify or read state that changes per user, per request, or per session. This includes database connections, user sessions, and calculated results stored temporarily.
  • Use :: for constants, helper functions, factory methods, and shared configuration values that remain the same regardless of how many objects exist.

In modern PHP development frameworks like Laravel, you will frequently see both patterns. For instance, accessing a model's relationship ($user->posts) uses ->, while accessing a helper class from the global namespace (Helper::formatDate()) uses ::. Recognizing this distinction ensures your code remains readable and adheres to the principle of least surprise.

Our network