تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Laravel and PHP0:00

They say that Laravel developers don't learn PHP, and that the framework abstracts everything. And there's a little grain of truth, because you can use Laravel for years and never go deeper than controllers, eloquent, and a couple of helpers. But Laravel doesn't prevent you from learning PHP, it's actually a giant readable PHP codebase, and it uses a lot of modern language features. So that's right, some PHP. To start, I want to create a couple of classes, just normal PHP classes.

Creating PHP Classes0:29

So that's right, some PHP. To start, I want to create a couple of classes, just normal PHP classes. And before we do anything else, if you haven't already, clear the optimized cache, it'll save you headaches. I know it will. All right, so let's make a class, and we're going to create two class. The first is going to called money, and it's obviously going to represent money . The second is going to be a billing calculator, and that is going to allow us

. The second is going to be a billing calculator, and that is going to allow us to calculate our money. So let's start with money. And money is what for me in US dollars, it is dollars and cents, but in a lot of financial software, you'll see money tracked as the lowest unit of that currency. So in my case, it would be since we're going to do the same thing. So we will have a public read only ints called since, but it's also very useful

So in my case, it would be since we're going to do the same thing. So we will have a public read only ints called since, but it's also very useful to know what kind of currency we are dealing with. So we'll also have that as an argument, and we'll just default it to USD. You can default it to whatever you want. Doesn't matter. Now, just right out of the bat, what have we done? We have created a class. We have a constructor that has automatic public properties, PHP features.

We have created a class. We have a constructor that has automatic public properties, PHP features. You will find this in Larry Bell's code base. You will also find this in so many other people's code base, because it's a common thing. It's PHP. We do it all the time. So what do we want to do with just money? This is just to represent money. I want to be able to format it as a string so that we can, you know, actually

This is just to represent money. I want to be able to format it as a string so that we can, you know, actually see what's going on. So let's call this formatted, and it's going to return a string. So we will return S sprint F, and the first thing we will output is going to be the currency. So that will be the first thing followed by the actual monetary value, but we need to divide this by 100 because we want the dollars and cents.

need to divide this by 100 because we want the dollars and cents. And the cents, of course, is two decimal points. So that's our format string there. The first thing that we will provide is the currency, because that is the first thing that we want to output. The second thing is going to be sense, not center sense, but we want to divide it by 100. And there we go.

it by 100. And there we go. So we have a class that represents money. And it's just a normal PHP class to where we have these automatic properties. We use those properties inside of our formatted method. And there we go. Nothing Larry Bell associated at all. So we're done with that. So let's open up billing calculator. And in this case, we don't necessarily need a construct.

Building Billing Calculator3:15

So let's open up billing calculator. And in this case, we don't necessarily need a construct. Basically what we want to do is take the money that's coming in, and we want to be able to calculate it for other things. Like, for example, we will have a yearly method. This is going to essentially take a monthly value, which would be our money. And it's going to multiply it by 12 so that we will have the monthly, and then we will have the yearly.

we will have the yearly. Let's call this yearly price. So we will accept money. We will return money. And all we are going to do is take the incoming amount. You will create a new money object, but we will take the incoming money sense and multiply it by 12. I didn't call it money.

it by 12. I didn't call it money. I called it monthly. That makes more sense. And then we will pass down the monthly currency so that everything stays as it should. So once again, we have a class. It's just a normal class. No special things. It's just normal PHP.

Demoing Money Classes4:21

No special things. It's just normal PHP. And we're done there. So now let's demo this. Let's create a new route. We have a ton of routes. It's going to bug me, but I'm not going to clean it up. And let's make this a get request. We'll just call the URL demo a, why not? Then we will have our function.

We'll just call the URL demo a, why not? Then we will have our function. Now we could go ahead and we could inject the billing calculator there. In fact, let's just go ahead and do that so that we'll start off with a monthly value. So we will new up money and let's say that this will be $25, which is two thousand five hundred cents. I wish that was two thousand five hundred dollars, but oh well. Then we will get the yearly and here we will use our calculator to get the

I wish that was two thousand five hundred dollars, but oh well. Then we will get the yearly and here we will use our calculator to get the yearly price of that monthly price. So then we can just return. We will say that the monthly value is the monthly. We will call the formatted method. Why didn't that pop up before we run this? I want to investigate that. So we will have yearly, which will be the yearly value formatted.

I want to investigate that. So we will have yearly, which will be the yearly value formatted. Well, okay. IntelliSense is at least showing something there. Monthly monthly monthly. Oh well, for whatever reason, the monthly values formatted method is not popping up for IntelliSense. So if something is wrong there, we're going to find it. And that's why we need using statements for money and okay, there we go. I'm happy now and especially because it runs just fine in the browser.

And that's why we need using statements for money and okay, there we go. I'm happy now and especially because it runs just fine in the browser. So we can see that the monthly is USD 25 yearly is USD 300. So that's working just fine. There's nothing Laravel associated except with the route, but you know, the underlying classes billing calculator money is just normal PHP classes. There we go. But now let's look at some enums. So let's make a new class, we'll call it domain and we'll call this post status

Using PHP Enums6:09

But now let's look at some enums. So let's make a new class, we'll call it domain and we'll call this post status . Now of course, this is going to create a class, which of course we will need to change to an enum. But this is going to allow us to create a backed enum. And we're going to back this as a string so that our case can be draft, which will have the backed value as draft, then we can have a published and then we could have

will have the backed value as draft, then we can have a published and then we could have an archived so that we have multiple enum values here. And once again, it's just PHP. There's nothing special here, but we can use it in our code. Let's create another route. In fact, let's just copy what we have there and paste it. Let's call this demo B. Now we don't necessarily need a parameter here. So we'll get rid of the billing calculator.

Let's call this demo B. Now we don't necessarily need a parameter here. So we'll get rid of the billing calculator. We don't need anything in here either. But let's say that we will get information from the query stream. So we will use the request and we want to validate that query string will call it status. And we will say that it's required, but it also needs to be one of our statuses . So that was post status class. Now if that passes validation, then we can take that value from post status

So that was post status class. Now if that passes validation, then we can take that value from post status from, and we are essentially explicitly converting it from a string to a post status. Now we do need to use statement to use enum here, because this is part of Lara vel's validation. But here we get the enum value based upon the status that was provided from the query string. And then let's just return, let's return the raw value, which would be the

string. And then let's just return, let's return the raw value, which would be the status from the query string. Let's also have the enum value, which would be the status variable. And then let's check the value. Let's say that if it's published, then we can say status is equal to post status published. So let's go to the browser. Let's go to demo B. So we'll add status equals archived.

So let's go to the browser. Let's go to demo B. So we'll add status equals archived. And what do we have? We have the raw value as archived. The enum value is archived is published false, but if we change status to published, then we see that the raw is published, the enum value is published, and it is indeed published. So once again, nothing Laravel specific, as far as this enum is concerned, it's just

Attributes and PHP Features8:46

So once again, nothing Laravel specific, as far as this enum is concerned, it's just a normal PHP enum that we were able to use inside of our Laravel code. So Laravel isn't hiding in a PHP, Laravel is embracing PHP. And that is explicitly true, thanks to this enum validation rule. But you know, there's, there's more to it than just those. What about attributes? So one attribute that I could show you just right off the bat is the use factory attribute. So I have post and I have post factory, but I can add or decorate.

factory attribute. So I have post and I have post factory, but I can add or decorate. I should use the correct term. I can decorate the post model class with the use factory attribute. And this essentially allows me to specify the factory class that I want to use for this model. And that would be the post factory class. There we go. Use factory is, of course, Laravel specific, but the language feature, the fact

There we go. Use factory is, of course, Laravel specific, but the language feature, the fact that I can use an attribute here is beautiful. And Laravel exposes many other attributes that we can use throughout the framework. A few other language features that we've used already, well, we use them all the time. Let's go back to our routes. We have closures, boom, right there or not right there.

Let's go back to our routes. We have closures, boom, right there or not right there. It's right there. We have a closure there. We have another closure there somewhere. We have an invocable controller, which is right here, resolve log controller. This is an invocable class, which is a PHP feature. So the real issue isn't that Laravel prevents PHP learning. I mean, because clearly it doesn't. It's that you can be productive without really being curious.

I mean, because clearly it doesn't. It's that you can be productive without really being curious. But if you do engage, if you do read the Laravel source code, Laravel ends up teaching you about PHP's object model about reflection and callables and modern syntax better than most tutorials ever will. Laravel doesn't hide PHP from you. It hands you a giant working example of PHP.

It hands you a giant working example of PHP.

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