در حال بارگذاری ...

Classes as Blueprints0:00

All right, let's get started with classes. And don't worry, I'll make this really easy. I like to think of classes sort of like blueprints in the programming world. And I'll give you a really basic example, but it really helps. So think about a person. If that were a class or a blueprint, what would that consist of? Well, yes, a person has a skull, a person has veins, you know, a person ideally has arms and legs, but also a person has a name, right? A person has an age, a person has a skillset, a person has interests. But notice at no point did I say a person has a friend named Joe,

A person has an age, a person has a skill set, a person has interests. But notice at no point did I say a person has a friend named Joe, or I didn't say a person has an interest called Guitar. No, they could have that, but that is unique to instances of a person, or particular people, but it doesn't describe a person in general. So now we're starting to see there's a bit of a distinction between the blueprint and the implementation. The blueprint is Person, the implementation is Jeffrey, or Jane, or Joe. And Joe has different interests from Jeffrey, and Jane has different friends from Sarah, right? So we have blueprints, and then we have implementations.

Defining a PHP Class1:15

And Joe has different interests from Jeffrey, and Jane has different friends from Sarah, right? So we have blueprints, and then we have implementations. Or in the programming world, we would think of classes versus objects or instances. All right, let me show you some examples here. In php, we can define a class by using the class keyword. Makes sense, doesn't it? So if we continue with that example of a person, we would define it like this. All right, so two things. First, by convention, the class name should start with a capital letter, and then we wrap the body within braces, just like any kind of conditional.

Finding Nouns for Classes1:47

First, by convention, the class name should start with a capital letter, and then we wrap the body within braces, just like any kind of conditional. Okay, so I want to show you a bunch of examples. And here's what I like to do. Find the nouns. It's a really good way to get started with classes. Now, I'm going to warn you up front, there is no rule that a class name has to be a noun. As it turns out, it can be anything you want. It can be a verb. But in general, and especially when you're getting started,

It can be a verb. But in general, and especially when you're getting started, find the nouns is a really good mantra to repeat to yourself. All right, so let's just think about something in the programming world. Well, everyone builds a blog, right? What would be the noun in that case? Well, you know what? It could just be blog. That might make sense. It could also be post.

That might make sense. It could also be Post. It could be Article. It could be a User who signs up for your blog. It could be Comment. It could be a Tag. It could be a Category. And you know what? As it turns out, if you were to build a full-fledged blogging application, you'd probably have all of the classes that I just mentioned.

Modeling a Post Class2:44

As it turns out, if you were to build a full-fledged blogging application, you'd probably have all of the classes that I just mentioned. All right, so now let's just go back to something really simple. I always start with Post because we all know what a Post is. We've all created a blog at some point in the past. Okay, so what would a Post consist of? Well, a title, right? Maybe an excerpt. Maybe a body. Maybe an author.

Maybe a body. Maybe an author. It could be associated with things like a Post can be assigned to tags. A Post can belong to a Category. But then a Post has behavior or things we can do with it. For example, would it make sense at some point to archive a Post? And the answer is absolutely. If this Post is no longer relevant, mark it as archived. So that behavior might be defined as a function. Or when we create a function inside of a class, we call it a method.

So that behavior might be defined as a function. Or when we create a function inside of a class, we call it a method. It's just a thing. Try to remember it. All right, so if I were to add a function here to archive the post, I'm going to make it nice and readable. archive. Okay, so now I can see, well, a post consists of a title and a body, but also it has behavior. And one of those behaviors is it can be archived.

but also it has behavior. And one of those behaviors is it can be archived. Cool. What are some other behaviors that we might assign to a Post? Ah, here's a fun one. Maybe a Post can be shared with others. In that case, we might have a method name called share. And who knows? Maybe that fires off an email. Maybe that notifies all of your followers that,

Maybe that fires off an email. Maybe that notifies all of your followers that, hey, JohnDoe has published a new Post. Pretty cool. And we can wrap that within a name of share. Here's some other things. Like what if we need to define the status of a Post? Is the Post in draft form? Is it scheduled? Is it published?

Is it scheduled? Is it published? Is it archived? That too, if you want, could be its own class called PostStatus. So this is what I mean. You can get pretty creative here. And a lot of your learning will stem from making small adjustments to figure out, well, what should be a plain old function? What should be a class? And what should be a method on a class?

What should be a class? And what should be a method on a class? And yeah, I'm just going to tell you right now, you won't figure that out until you tinker around and make small little changes and you realize, oh, no, that doesn't work. This should be here. This should be there. You just got to put in the time and I promise you're going to get it. All right, let's keep going. How about class Comment this time?

Designing Comment Methods5:13

All right, let's keep going. How about class Comment this time? Well, what are some things that we might do with a Comment? Ah, how about this? A Comment can respond to another Comment, right? So imagine that Joe replies to a blog post and he says, this is the worst post that I've ever read in all my years. And maybe Jane wants to respond to Joe and she wants to say, hey, Joe, you're a piece of crap, okay? Well, how do we define or how do we represent that interaction or that behavior?

hey, Joe, you're a piece of crap, okay? Well, how do we define or how do we represent that interaction or that behavior? And this is one of my favorite things about defining classes is you do sort of become a designer. Even if you don't know art, even if you don't know how to use Photoshop or Illustrator or whatever the cool kids are using these days, you get to be a designer when it comes to the API that you choose. So when Jane responds to Joe, do we want to use the method respond? That would be fine. Do we want to use a method named reply?

That would be fine. Do we want to use a method named reply? Do we want the method name to also be comment? I don't know. And what's fun is you get to work it out and see what makes the best sense. And often what makes the best sense is when you say it out loud, what comes out, right? And if you can extract those verbs, then you should be good. So yeah, I keep saying responds. I'm going to reach for that one. I think that's pretty good.

I'm going to reach for that one. I think that's pretty good. So here's what I recommend you do. When you are defining your classes and you're trying to figure out what your method name should be, speak out loud. Even if it's embarrassing, just talk out loud and describe what should this do? What is the behavior? And I want you to listen very closely to the verbs that come out of your mouth. If you can, take those verbs and insert them into your code. So remember, just as classes, well, we want to look for the nouns, right?

Nouns vs Verbs Recap6:56

If you can, take those verbs and insert them into your code. So remember, just as classes, well, we want to look for the nouns, right? Well, for methods, we want to look for the verbs. Now, remember, we can break these rules whenever we want. But as an initial guideline, to say it again, classes correspond to nouns and methods can correspond to verbs, okay? So yeah, what are some other things just to wrap up this introductory video? And then I'd like you to take an exam. Well, what about something like a notification? I want to notify you that we've made a new post.

Well, what about something like a Notification? I want to notify you that we've made a new Post. All right, let's have a class called Notification. Excellent. What about maybe if you have read 10 blog posts in my app, you get an AchievementBadge? Everything requires an AchievementBadge these days. Awesome. Let's create an AchievementBadge class. And notice in this case, we have two words or a compound word.

Let's create an AchievementBadge class. And notice in this case, we have two words or a compound word. That's fine. Again, once you become a little more seasoned, you might even have classes that themselves are sort of like verbs. But that's a bit more advanced, and I'd rather you not do that at this stage. What else? Maybe you're building like a photo application like Flickr or Google Photos. All right, maybe you have a class called Photo. And maybe the behavior on Photo is that we can optimize it.

All right, maybe you have a class called Photo. And maybe the behavior on Photo is that we can optimize it. Maybe it can be converted into a different format. Maybe we can use AI to enhance the photo. We can upscale it. So again, I'm speaking out loud and words coming out of my mouth are enhance, upscale, and I'm listening very closely to those verbs. So maybe we have a method name called upscale. Okay, so that's going to do it for this introductory video. I understand at this point, it's probably still confusing.

Okay, so that's going to do it for this introductory video. I understand at this point, it's probably still confusing. So for now, I just want you to think about the general syntax and that mantra of class can be noun and method can be verb. All right, so if you're a subscriber, be sure to take the exam below, and that'll put your fingers to the test. And when you're ready, let's move on to the next episode, where we will talk about objects.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟