Lessons: 18Length: 2.3 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.3 Class Constants and Internal Reference

In this video, we'll have a brief look at constants. And we'll also see how to reference properties, methods and constants from within a class itself.

2.3 Class Constants and Internal Reference

Hi, and welcome back to PHP OOP fundamentals. Now in the previous lesson, we had a look at class properties and methods. In this video, we'll have a brief look at constants, and we'll also see how to reference properties, methods and constants from within a class itself. Sometimes you will want to define variables with a set value, so a value that cannot change. Outside of a class you can define those as a constant. For instance, if I want to define the minimum number of characters for a user password, I could do something like define minchars. And just set that to eight. Now you can define constants inside of a class as well. The difference is that a class constant, is only accessible to the class itself. In the case of our min class constants, that's a good things because Think about it. That constant is only accessible to our User class. You can use a constant of the same name anywhere else inside of your app, without having name collisions. Now, this is how to define a class constant. You use the key word const, and I'll just give this an extra space. Then you supply the name of the constant, and set its value. And although you can use any valid string as a name for a constant, its often considered good practice to use upper case characters. Okay. So now that we've defined the constant, let's just put it to use. I'll create a public function called setPassword, and let's just pass a string to that. Now prior to setting the password, we need to validated it, so let's set an if statemnt here. Check against the length of that string that we just passed. Now if it's smaller than our minimal characters, we should throw an exception. So how do we reference this constant here, we can't just do mincharas, because that would throw an exception itself. Instead you reference the class itself, by using the keyword self. Then followed by a double colon, which is also known as the scope resolution operator. And then followed by the name of the constants. Now this is how you refer to a class constant within the class itself.. And let's just correct this mistake here, because we want it to work, okay? Now if our password is too short, we want to throw a new exception. And as you can see, that's an object itself. New, exception. And we'll just pass in a warning, something like the password should be at least min characters long. Now let's just try and see if that works. We'll go back to our index.php, remove all of this, and we'll just call Yost and then the object operator, and then set password, and pass in a four character string, which is too short, let's have a look,and see how that looks in a browser. And there you have it, there's our message right there. So let's see what happens if we use a slightly longer password, like so, and the exception is gone. So that's good. Now one thing about the double colon here, it's also referred to as, i can't pronounce this. Well if you've ever seen a PHP error thrown like this, you know they're actually talking about the double colon. Okay. So now if the password, pass validation, we want to set it to a hash, but how do we access the email property? And now this is where it gets confusing. where as we use the self keyword for constants, we use the this keyword properties and methods. So that this keyword, followed by the object operator, followed by the actual property. Now let's just make that that equal to a hash, let's say, a 256 or something, and I will take this string and pass that in, like so. Now return to our index.php to see if that's all worked, let's do a dump of the entire objects like so, and see what that gives us. And there you have it, there's a hash password. So in retrospect, the two most important things we covered in this video. This is the way you set class constants. This is the way you reference class constants within the class itself. And this is the way you reference properties and methods within the class itself. That's all for now, I'll see you in the next video.

Back to the top