Join Kevin Skoglund for an in-depth discussion in this video Class constants, part of PHP: Object-Oriented Programming.
- [Instructor] In this movie, we're going to learn about class constants. You've probably been using constants before in PHP. They're used for values which don't change. Well, class constants are just for class values which don't change. The way that we define them in our class is we use the keyword const, C-O-N-S-T, followed by the name in all capital letters, just like we do with normal constants. The value that we set a constant to can be something simple like a string, or a number, or an array, or it can contain mathematical expressions.
If you're using a new version of PHP that is something later than 7.1, then you can also put a visibility modifier in front of that const keyword in order to make something public, protected, or private. Prior to 7.1 and by default, it's going to be set to public. So constants are available publicly outside the class, as well as inside the class. The way that you reference those constants is using ClassName:: followed by the name of the constant or self:: followed by the name of the constant.
So they are extremely similar to static properties. The difference is that a static property has values that can be changed, while constant values cannot. Here's an example of a class or clock, and you can see that it has a constant defined for DAY_IN_SECONDS, all capitals. There's no dollar sign in front of it when we have a constant. We have a variable, we put the dollar sign there. Having it all caps with no dollar sign tells us this is a constant. And I'm using a visibility modifier in front of it to make it public, and then I'm setting it equal to a mathematical expression.
What is a day in seconds? Well, there's 60 seconds in a minute, 60 minutes in an hour, and 24 hours in a day. So this tells me the number of day in seconds. You'll see below the class definition that I'm able to reference the constant by calling Clock::DAY_IN_SECONDS, and it returns the value back because it's public. I also can refer to it using self, and you see that I do that inside the method for tomorrow. This is not a static method. It's just a normal method called tomorrow, and it's going to return back the current time plus whatever day is in seconds, and that'll give me the time tomorrow.
So at the bottom, you can see that I created a new instance of the clock, and then I call its tomorrow method, and it returns back the time tomorrow. Let's put the same example in our sandbox to try it. So let's add a new file to our sandbox. I'm going to Control + click on this and choose New File. Let's call it class_constants.php, and we'll put up our php tags, and let's try that example that we just had where we had a class called clock.
And inside, we're going to have a public constant called DAY_IN_SECONDS. That's going to be equal to 60 times 60 times 24. Okay, now let's come down here and let's try this out. Let's have echo Clock:: and then I'm just going to copy this, paste it right there, and then at the end, let's put our br tags, so it will be ready for a new line after that. Let's just try that much out.
Let's come over here. Instead of static modifier, it's going to be class_constants. There you go, you see the result. Alright, so now, let's write something that'll use that internally. So I'm going to make a public function called tomorrow, and tomorrow is just going to return back time, which is a regular PHP function, returns the time in Unix seconds, it's Unix time, the number of seconds since 1970, and we're going to add that with self::DAY_IN_SECONDS, and then down here, I'm going to need to create a new instance of the clock, new Clock, and then we'll ask that clock to call its instance method tomorrow, and we're going to echo that, or it won't show anything, and we'll put a br tag at the end.
Alright, let's go back over here, let's try it out. Let's reload our page and there you go, you get there result. Now, just for fun, let's try this out. Let's take this away, the self, so we just have DAY_IN_SECONDS. Let's save it, come back, and reload the page. Notice undefined constant. So we need to have self in front of it, so that PHP knows that we're talking about the class constant DAY_IN_SECONDS.
- 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