Monday , 20 November 2017
Home / Programming / PHP / Scope Resolution Operator – PHP OOP Tutorial
Scope Resolution Operator
Scope Resolution Operator

Scope Resolution Operator – PHP OOP Tutorial

This tutorial is a part of the PHP OOP tutorial series. If you would like to view the complete list of tutorials under this series and the order you should follow, then click here.http://jaskokoyn.com/php-oop-tutorial-series/ 

The scope resolution operator may sound intimidating, but it’s actually quite simple to use. What it basically does is it allows you to access static, constant, or overridden methods and properties of a class. We haven’t discussed static and constant properties and methods yet. We will soon in a another tutorial, but for now, we can use it to access overridden properties and methods.

Our Pet class, runs the constructor method which creates the date of birth for the pet. We don’t have this in our CanadianPet class’s constructor method.We can’t use the Pet’s constructor method because our CanadianPet can’t have the letter A in our pet’s name.  So, how do we get around this?

Using the scope resolution operator ( :: )

We can simply include the date of birth line in our CanadianPet’s constructor method or we can run the Parent’s constructor method along it’s own constructor method. Let’s take a look on how to do this. Open up your CanadianPet class and add this constructor method inside it.

Our constructor method is pretty simple. We’re introducing a new keyword here which is parent. The parent keyword basically refers to the class that’s being extended. In our case, it’s the Pet class. To access an overriden method, we must use the scope resolution operator, which is a double colon. Think of it as the ->, but it can only access overridden methods and properties. Next, we type in the name of the method or property we want to use. In our case, want to use the construct method. We pass in the name of the Pet because the Pet’s constructor method needs this.

After we’ve done that, we then fix the pet name and check if it contains the letter A. Now, we have the name set and the date of birth set.

You get the best of both worlds with the scope resolution operator. Now you can toggle between overridden and non overridden methods and properties.

Conclusion

You can use the scope resolution operator to access the parent methods and properties. You can also use it to access constants and static properties. We’ll learn about these 2 later. If you would like more information about the scope resolution operator, then click here.

http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php

About Jasko Koyn

Check Also

Abstract Classes

Abstract Classes – PHP OOP Tutorial

Today, we’ll be discussing what abstract classes are and how we could use them. There …

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link