Join Kevin Skoglund for an in-depth discussion in this video The static modifier, part of PHP: Object-Oriented Programming.
- In this chapter, we will learn about static properties and methods. Static properties and methods are for behaviors that are related to the class generally, or the class as a whole. These are behaviors that are not tied to any particular instance of the class. For this reason, they're often referred to as class properties or class methods. We're going to have access to them by calling them directly on the class without having to have an instance of the class first. In order to define them, we're going to use the keyword static. So for example, if I have a student class, I can have a static property for grades by putting the static keyword in front of it.
In the same place where we put our visibility modifiers in the last chapter, and then I can have a static method called motto by putting static in front of the word function. The idea is the grades property is holding data that is related to the class as a whole. It's not specific to any one instance of the class. Presumably if I had an instance of student, that student would be one of these grades, freshman, sophomore, junior, senior, but it makes sense that we would have an idea of all grades that are possible stored in the class as well.
That's knowledge that the class has about the possible choices, even though an instance is only going to pick one of them. Even though it's a little silly of an example, the motto is going to be something that belongs to the class so that all students have the same motto, it's not a motto that's unique to each and every student. Once we've defined our static properties and methods, we're going to need to use a different syntax in order to reference them. We can't use the arrow notation we've been using when working with instances. Instead we're going to use the class name, followed by two colons, and then the name of the property or the method.
Notice that I have a dollar sign in front of the property name. That's very important. That's different from what we were working with with arrow notation. When we had arrow notation, we omitted the dollar sign, but when we're working with static properties, we must include it. In previous chapters, our method code was able to make use of a special pseudo variable called this. Our static methods cannot use it, and that makes sense because this refers to this instance, and we don't have an instance here.
We haven't created an instance. We're talking to the class directly, so it can't refer to a specific instance using the this variable. Instead, if a static method wants to refer to the current class, it can either do it by using the class name and two colons, or, even better, it can use the special keyword self, so self, colon, colon, and then the property or the method. It's considered a best practice to use the class name when you're outside the class and to use self when you're writing code inside the class.
Part of the reason that's important is that we can also combine the static modifier with our visibility modifiers, and if we make a property or a method public, it's going to be available both to student, colon, colon, and to self, colon, colon, but if we make it private, then we can call it from inside the class using self, but we will not be able to call it from outside the class using student. Here's an example that demonstrates that. Notice that I'm using my visibility modifiers with my static modifier.
The order that they go in doesn't matter. I like to put my visibility modifiers first. You can see that grades has been marked as being public, so therefore we're able to reference it outside the class definition and ask for the grades at index position zero and it returns back freshman. Total students, though, has been marked as being private, so when we try and make reference to that from outside the class using student, colon, colon, it comes back with an error saying we cannot access this private property. However, I can have a public static method called count, which can reference it by using that self keyword, and then I can call that public static method in order to get back the value.
So far, all of our code has been talking directly to the class without instantiating any objects. If we did instantiate an object of this class, it's important to realize that we cannot access the static properties from that instance. The static methods are accessible from an instance, but it's a quirk of PHP and you shouldn't do it. It's considered a bad practice. PHP 5 will do it, but will also return a warning. PHP 7 will do it, but returns a deprecation warning, which essentially says you can do this, but support for it may go away in the future, so beware.
Let's try an example. Inside our sandbox, in the exercise files, I've included a new file called static_modifiers.php, where I essentially just define the student class that we were looking at in the slides. I gave it a public static property for grades, and I gave it a private static property for total students. You can see that I've got a public static method for motto and a public static method for count. Let's just try some of those out real quick.
Let's start by just trying to reference those. So let's do echo, we're going to call this student, we're going to use colon, colon, and then a dollar sign. Very important. We're going to ask it for the grades that index position zero. We'll put a VR tag after it just so that it goes to a new line, and let's also just copy that, but instead of the grades property, let's call the motto method. Okay, so let's save that, go back over here, and let's try it.
Going to be static_modifiers.php. There we go, freshman, and to learn PHP OOP. So there you go. We get back both those values. We were able to use double colon notation to reference both of those. Now let's try another one here where we try and go directly to dollar sign total students. Let's see what that gives us back. Let's reload our page.
We get back an error. It's a private property and we can't access it, alright, we knew that, let's mark here, error, and I'll just comment that line out. Just grab this line, copy it. Instead we asked for count. It can access it. Even though it's a private property, we're using self and it's able to get there, so let's ask for that value. It comes back and says it's zero. Alright, now there's also another method here called add student. Maybe every time I create a new student, I'm going to increment this value of total students, and then I can keep a running count of the students, so it's going to call self, colon, colon, total students, plus, plus, we'll increment it, so now I can call that down here.
Let's try student, colon, colon, add student. It's public. I can certainly call it. And then let's ask for the count again. Save it, come back over and reload the page, and there we go, zero and one. So that's how static or class property and methods work. Static properties and methods are an important tool in your PHP object oriented toolbox.
- Defining classes
- Calling methods
- Class inheritance
- Extending and overriding classes
- Accessing and controlling access to properties and methods
- Static properties and methods
- Magic methods: constructor, destructor, and clone
- Creating a PHP OOP project