I remember when I first came across the mysterious double-colon (::), also known as the Scope Resolution Operator. I was experimenting with a php library I hadn’t written, but was asked to extend, and at the time I had no idea what classes were, how they worked, or how they helped.
To try and figure out what the double-colon did I remember I peppered Google search with questions like, “What is the :: ?” or “what does the :: symbol mean?” Those searches proved useless. At the time php5 was still very new and most PHP coders were writing procedural code rather than object-oriented code.
Now that I use object-oriented design every day I thought I would explain what the double colon :: means, just in case there are any new PHP programmers, like I was, and are confused by the mysterious double colon.
When and how to use the double-colon:
You use the double-colon to access static methods from a class. To get the color of the bear, from the example class I wrote about here, you would write the following.
I will break the above line of code into its three parts.
1. Bear – First you write the name of the class you are referring to. In this case Bear.
2. :: – Then you use the double-colon because the method is declared as static.
3. getColor(); – Then you write the name of the function you are calling that exists within that class.
And that’s the basic use of the double-colon to call a method from within a class.
The double-colon is simply a way of referencing the class object and accessing the static methods (functions) and constants within that class. If you are new to this I just said a lot. I explain these things briefly here.