Hi guys,

Just wanna ask what the "::" is for? ex.

CRM_ContactsCommon::get_my_record()

Correct me if I'm wrong, the CRM_ContactsCommon is a class and the get_my_record is a function inside the class. Now, is the "::" works the same way like in java to access a function of a class?

Thank you in advance.

Dani AI

Generated

Good question — nice follow-ups from and . In PHP the double-colon is the scope‑resolution operator (official token name: Paamayim Nekudotayim). It’s the syntax the language uses when you want to reference class-level items (class constants, static properties and static methods) or special class-resolution keywords such as self::, parent::, static:: and the ::class name literal. (php.net)

A common point of confusion is how self:: differs from static:: (late static binding). self:: resolves to the class where the method is defined; static:: resolves to the class that was actually called at runtime. Example:

<?php
class Base {
    public static function who() { echo __CLASS__ . PHP_EOL; }
    public static function test() { static::who(); } // late static binding
}
class Child extends Base {
    public static function who() { echo __CLASS__ . PHP_EOL; }
}
Child::test(); // prints "Child"
?>

Use static:: when you want subclass overrides to be honored. (php.net)

Compatibility heads‑up: older PHP versions tolerated calling non‑static methods with :: (with warnings). That was deprecated in PHP 7 and, starting in PHP 8, calling a non‑static method statically is a fatal error — don’t rely on that behavior. If you see code that does that, either instantiate the class or make the method explicitly static. (php.net)

Practical advice: use static methods for stateless helpers and constants only. If a method needs $this or mutable state, instantiate the class — it’s clearer, easier to unit‑test and less error‑prone.

Recommended Answers

All 5 Replies

In PHP :: is used to denote a static class i.e. one that does not need the class to be instantiated first.
If the method is not static you need to instatiate the class and then access the method via $class->someMethod().

Thank you hericles for the brief description :)
Thank you pixelsoul for the link, I really dont know what to call the colon-colon ( :: ).

Just checked the link, and....yes you're right,...
From now on I'll that too as double colon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.