Thursday , 17 August 2017
Home / Programming / PHP / Static Methods and Properties – PHP OOP Tutorial
Static Methods and Properties
Static Methods and Properties

Static Methods and Properties – 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/ 

Let’s learn how to create static methods and properties. Anything that’s static is basically something that can be accessed without needing an instantiation of the class. You also have the advantage of having a value that is consistent through each instance of the class. Let’s take a look at this.

Creating Static Properties

Inside our Pet Class, add this bit of code.

The static keyword tells PHP that this is a static property. We’re creating a property that will hold the value for the amount of pets created. Since this property is static, it’ll always be the same value throughout all instances of the Pet class. You don’t have to define the scope of the property. By default, static properties are set to public. We’ll see how this works in a bit.

In our construct method, add this line of code.

As you can see, we’re not using the object operator, but the scope resolution operator to access this property. You must use the scope resolution operator or else you’ll get an error. The self keyword refers to the class itself.

Static Methods

Lastly, let’s create a static method that will return the amount of pets created when we need it.

Once again, we’re using the static keyword to tell PHP this is a static method.  We use the self keyword again to grab the value. You’ll notice that we’re not omitting the $ from the property name like we would a constant. To grab static properties, you must use the $. We then return this value.

In our index.php file, update your code to this.

Open this file in your web browser and you should see 2. As you can see, when we create a new instance of the Pet class, the $petsCreated property is updated by 1. By updating the value in one instance of the class, any other instance using the same class is then updated to correspond with the latest value.

I also mentioned you can access this class without creating an instance of this class. We can update our last line of code to this.

Conclusion

As you can see, static methods and properties can be very useful and powerful when creating classes. You gain the power of having a constant method or property throughout all instances while also being able to change the value. If you would like more information about static methods and properties, then go here.

http://php.net/manual/en/language.oop5.static.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 …

2 comments

  1. This is a good tutorial that has potential to be a great tutorial with some adjustments. It is better than many I’ve seen on the subject. It answered some questions I had and clarified some things. Nice job!
  2. Hello nice job….. Really loved this….. Keep up the good work….

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