- Overview
- Transcript
5.1 Abstract Classes
An abstract class is a class that is meant to be a parent class only. You cannot create an object from an abstract class. So it’s the kind of class you can use when you want to force developers to extend that class, rather than use it directly. Let’s have a look to see how that works.
1.Welcome1 lesson, 01:07
1.1Welcome01:07
2.The Absolute Basics of OOP5 lessons, 22:53
2.1What Is OOP?04:40
2.2Classes vs Objects05:42
2.3Class Constants and Internal Reference04:19
2.4Public vs Private Scope06:05
2.5Copying vs Cloning Objects02:07
3.Digging a Little Deeper Into OOP5 lessons, 43:52
3.1The Single Responsibility Principle15:18
3.2Magic Methods11:16
3.3Autoloading Through SPL03:37
3.4Working With Namespaces05:14
3.5Autoloading With PSR-008:27
4.OOP inheritance2 lessons, 12:02
4.1Class Inheritance And Protected Scope09:22
4.2Overriding Parent Methods02:40
5.OOP Abstractions5 lessons, 1:00:05
5.1Abstract Classes05:59
5.2Interfaces14:53
5.3Static Properties And Methods06:49
5.4Traits14:44
5.5Dependency Injection17:40
5.1 Abstract Classes
Hello, and welcome back to PHP OOP Fundamentals. In this video, we'll have a look at abstract classes. An abstract class is a class that is meant to be a parent class only. You cannot create an object from an abstract class. So it's the kind of class you can use when you want to force developers to extend that class, rather than to use it directly. Let's have a look to see how that works. Say we want to our app to have two types of users administrators and members. Now they both share a login feature, but they need to differ for just a little. Let's say login's for administrators need to be loading an administrator table. So we can review which administrator has logged in and when. On the other hand, when a member logs in we need to create a flag in a table for online member, so that we can see which member is online and which is not. But other than that, the log in methods need to do exactly the same. So let's make that happen. First of all, we'll open up our user class. And turn it into an abstract class by prepending it with the word abstract. As of now, we cannot create an object from this class directly. Let's try and see if that works. We'll just open up index.php, and here we'll take our variable called Joos, t and make that equal to a new AddmeAppUser. So if all goes well, this should throw and error. And sure enough, it does. Now, let's have a look at our Administrator class. That already extends the User class. Now the User class has a login method here at the bottom. For now, it just returns a message to say we're logging in a user. When you create an administrator and you have him log in, then you use all the log in functionality from the parent class. So that is from the abstract user class. And then, you can tweak it and append to it, just that little bit. In our case, we'll just say we log this action into an administrator stable. Now, let's go back to our index.php. And instead of an acmeAppUser we'll create an acmeAdministrator like so. And just to be a little more specific, we'll change the name of the variable in to administrator. And then, we'll echo the output of the administrator log in method like so. And here, you can see that like expected, we're first executing all the logic in the abstract user class followed by the logic in the actual administrator class. Okay? Let's quickly create a second user type called member. I'll just copy and paste this administrator class here and rename that to member.php. We'll just open it up and the class name, of course, need to be member. We'll just remove this altogether and this as well. And here, we'll just execute our member specific feature, which is to sit a flag in an online member table. And let's see what happens if we go back to index.php, and we'll just create a second user. Let's just store that one inside a member variable, that one of course be a new acme app member. And then once we have that member, let's just make him login like so. And just to avoid confusion, I'll just comment out this two lines here. Let's have a look at the browser and refresh, there you go. Executing the user login once more, and then after that, we'll set a flag in the online members table. So to recap, you could say an abstract class is a bit like a partially built class. It has certain properties defined, and it can also have certain methods defined already. And what's more, these methods can already execute some core functionality. Now, there's just one last thing to take note of, abstracts is a keyword. And you can not only use it to define the type of class, but you can for instance also use it to define the type. The method if I prepin this public function with the keyword abstract. Then, the actual method is only a placeholder for children classes to implement. It is not allowed to do anything by itself, so it cannot have a body. You see if we check in the browser, we get a fatal error here. Warning us that we are trying to call the Logan method on Acme\App/User, but that it cannot contain a body because it is an abstract function. Instead, the way to implement that would be to have it followed by a semicolon, and then this would just be a placeholder. And I'll just remove public here. And we would need to completely define that method in our child classes. So in here, we could not call parent logging because that's just a place holder and you can't call that directly. So then here, we would just need to do stuff and then do our other stuff. And then inside of our member class we would have to remove this line, because this is trying to call a placeholder, which doesn't make sense. And that will have to be a complete rewrite for a login method. Something like this. As a matter of fact, because I created this abstract placeholder method in the parent class, I must have it defined in the children class as well. Let's see what happens if I remove it here, and that will give us an error as well. You see, PHP throws a fatal error because our abstract login method is not defined as well in our child member class. Okay? I'll just go back and clean this up just a little bit, so the member classes like it was before. And let's just undue these changes as well, because this is what we want. We do want the login method to contain functionality that we can use in all child classes. I'll leave you with that, and see you in the next video.







