Join Kevin Skoglund for an in-depth discussion in this video Define a class, part of PHP: Object-Oriented Programming.
- [Instructor] In this movie we will learn how to define a class in PHP. A class provides the definition of an object. It describes it and it serves as a template for creating new objects. Every class definition begins with the keyword class followed by the class name. Then you have curly braces that surround the class definition. In the first example, the class is called Person, in the second it's ProductImage. There are a few best practices that we should observe when declaring classes.
The first is that you want to capitalize and camelcase the class names. Camelcase means that you use uppercase for the first letter and then the other words would also be capitalized as well. ProductImage was an example of that on the last slide. Both the P and the I are capitalized. Class names should also use singular nouns. Don't use plurals because we're defining what a single object would look like. It's also a best practice to define each class in a separate file.
In our final project, we're going to be doing that for sure. When we're working with it in our sandbox, frequently we'll be putting some other code into the class definition file as well just for convenience and to keep it isolated as a single example but it's a best practice to keep class definitions in their own file and then you would group those class files together in a directory, frequently that's a directory that's just simply called classes. We'll learn more about writing the definition for our class in the following movies but before we do, there are two functions for classes that we should know about.
Whenever we declare a class, PHP keeps track of the fact that it's been declared and we can call get_declared_classes which will return an array of the classes that PHP knows about. Then we also have another function called class_exists which takes a string as an argument and returns true or false depending on whether a class is in that array of declared classes. Let's try these out in our oop_sandbox. Here I am in my project and I'm actually going to just control click on top of the sandbox and choose New File and that'll create a new file in the project.
I'm just going to simply call it class_example.php and you'll see that it appears right here inside my sandbox. Once I'm there, then I can put in my class definition. Now, we have to do this inside PHP tags because of course we're working in PHP and we define a class or declare a class by simply having class and then whatever the name of the class is. I'm going to use Student. So again, remember, you want to use singular nouns and if you have something that's sort of a hyphenated or multi-word noun like StudentProfile or StudentSchedule you want to make sure that you use capitals for the second word as well.
I'm just going to use Student for this one and now I have a class definition. Now, it's not very exciting, there's nothing that's in there yet. We're going to learn how to make it more exciting later but that's all it takes to actually define a class and we can check that by getting that array using get_declared_classes and that will return an array of the classes and then we can actually look at that. Let's do that with echo "Classes" and let's add that to our array to actually turn the array into a string so we can output it.
We're going to use implode and then I'll use classes, then let's use a br tag at the end just to make it look nice because down here I also want to use that class_exists. I'm going to do a couple of tests here. Let's do class_names. We're going to make a new array and I'm going to have Product and Student and let's do lowercase student and then let's do a loop.
Foreach we'll do class_names as class_name and then inside our loop let's test each one of those items to see if the class exists, so we'll use class_exists, we'll pass in the string for the class_name and if it exists, and then let's do an else for when it doesn't exist. If it does exist, we'll echo back, let's interpolate a string here for class_name is a declared class and let's put a br tag at the end and then we'll just copy that line, we'll paste it down here is not a declared class.
So, that'll just loop through and tell us whether something is a declared class or not. Right, let's save this file and let's go try it out. Let's come over here to Firefox and instead of php_test, we're now going to do class_example.php and there we go. Now, here's the list of classes. Notice that there's a lot of classes built into PHP, not just our Student class which is here at the end but all these other classes are there as well. These are built-in classes, these are functionality.
Yours may be different. You may have different libraries installed in your PHP, so don't panic if it's not the same but you'll see that there are a lot of classes there as well as our class and then we have Product is not a declared class, Student is a declared class and notice this, lowercase student is also considered a declared class, so it doesn't matter whether the string is capitalized or not, it's case insensitive when it checks, so it is still considered a declared class. So, now we know how to define a class but we don't know how to actually use a class and that's what we're going to learn how to do in the next movie when we create new instances of this class.
- 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