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

Introducing Anonymous Classes0:00

Now we know that we can create anonymous functions in php, or we would call them closures. However, in PHP 7, we can also create anonymous classes. Definitely not for everything, but you might get some use out of them in some situations. For example, in the documentation, they use this idea of a logger. So let's say we have an application, and then we'll have some kind of logger instance there. Let's go ahead and add a setter for that, in fact. Clear this out, and maybe make that public. Okay, so when we set a logger, we'll reference an interface there, and let's set that up. public function log, and accept some kind of message. Okay, now traditionally, you'd have your concrete implementations, right?

Building Logger Example0:37

public function log, and accept some kind of message. Okay, now traditionally, you'd have your concrete implementations, right? So you could say class, and how about something like TerminalLogger, where we just var_dump it. Okay, let's var_dump the message, and I'll have that implement our logger interface, or contract. Okay, great. Nothing new or unique to PHP 7 here. So now, while we could create a new instance of our application, we could set our logger, and make that equal to a new TerminalLogger. That's what we want to use right now. And then finally, when we trigger some kind of, we'll call it action, among other things, we will log some data. This, logger, log, doing things.

And then finally, when we trigger some kind of action, among other things, we will log some data. This, logger, log, doing things. Okay, makes sense. New up our app, set a logger equal to a TerminalLogger, and then when we trigger some kind of action, among other things, we log some data. Let's try this out. App, action. And now if I switch to the terminal and trigger this, sure enough it works, right? Okay, well now, what if we wanted to use an anonymous class? That could be a possibility as well.

Using an Anonymous Logger1:48

Okay, well now, what if we wanted to use an anonymous class? That could be a possibility as well. For example, in this situation, maybe it's just not necessary, maybe we are debugging, I don't know, maybe we're testing. In that situation, maybe it doesn't require its own class and place in your file system. Instead, we could just new up a class right here. This is brand new to php 7. So now when we log, we can do whatever we want. In this case, like we said, var_dump the message. Sort of interesting, right? Now in this case, in fact, it's actually going to fail.

Guidelines and Testing Use2:50

Only reach for an anonymous class when you very much understand why you're doing it. You're doing it for testing purposes. You're doing it maybe for the ability to swap strategies out on the fly and things like that. Main point being, just ensure you know what you're doing. Now outside of that though, I think this can be incredibly useful for the purposes of testing. So for example, you may know about mockery. If you don't, it doesn't matter. It's just a mocking library. So maybe we are mocking some kind of User class, right? And we want to say User should receive maybe their paymentHistory.

So maybe we are mocking some kind of User class, right? And we want to say user should receive maybe their payment history. However, in production, that method will actually send off some kind of API request to a billing service or something like that. So we want to mock it out. Okay, well, we could say should receive history and return. And maybe we need to mock out a History object or maybe you have a History collection. Well, of course, you could just mock that yourself. Or if it's useful or you need to stub things out in a specific way, you could return an anonymous class. Like so. And who knows, maybe you have like a BillingHistory that will return active or something like that.

Transformer Pattern Example3:55

Like so. And who knows, maybe you have like a billing history that will return active or something like that. All right, how about one more example and then I think we pretty much get the basic idea. I was talking on Twitter about this, looking for some good examples. And one of the people who helped develop this RFC, Phil Sturgeon, gave me an example for one of his packages where you have to pass in a transformer, which is just an object with a single method, transform. So for example, maybe you have a BookTransformer. And within there, you have a transform method that returns an array of some sort. It just maps keys to the associated properties on an object or something like that. All right, well, if we were to new up an item, maybe that requires the transformer.

It just maps keys to the associated properties on an object or something like that. All right, well, if we were to new up an item, maybe that requires the transformer. Okay, well, what you could do is new up an item and then new up a BookTransformer as that first argument, right? That would do the trick. Or alternatively, since we can now use anonymous classes, if it makes sense for your project, you could do something just like this and you'll still get the same end result. All right, so that'll do it for anonymous classes in PHP 7.

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