The Scope Resolution Operator (::) explained

Double Colon object oriented programmingI 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.

6 Comments on "The Scope Resolution Operator (::) explained"

  1. O its realy very simple briefing. I’m new in php programing . Now I understand the concept of scope resolution operator. Please share more about object oriented in php . Thanks


    1. Hi Talha, welcome to PHP and thanks for the comment. Will be sure to add some more tutorials about Object-Oriented PHP.


  2. thank you So much. I am a very beginner of php coding. your post helped a lot me building up my php world.


  3. “You use the double-colon to access static methods from a function”

    Shouldn’t be “You use the double-colon to access static methods from a Class”


    1. Hi Shuvro, you are correct. Thanks for alerting me of this mistake. It’s updated now. Cheers


Leave a Reply to Jonathan Whiting Cancel reply

Your email address will not be published.