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

Problem: Array to Object0:00

I recently ran into an issue where I wanted to upgrade an array to a first-class citizen. So for example, if I had a message array here that consisted of a title, a body, a boolean here, whether it's an important message, you get the idea. I decided after I had pushed this to production and users had made use of it, I decided that I wanted to upgrade this to a first-class citizen, and instead I wanted a dedicated message object. Now normally this would be no problem, but the issue was that because it had already been deployed and people were making use of it, well, within their views, they expected an array. So they would end up having a collection of this message type, and they would do something

an array. So they would end up having a collection of this message type, and they would do something along the lines of, this is their Blade file, they would do foreach $messages as $message, and then wherever necessary, they would echo out the $message's title. So you can see what the issue is. If I decide that I want to change the array to a Message instance, well, in their views, once they pull in that code, they would have to change them to being $message title. And normally for a breaking change, this is no issue whatsoever. But if you don't want it to be a breaking change, and you don't want the end user to even notice a difference, what do you do in these cases?

Solution: Implement ArrayAccess1:10

But if you don't want it to be a breaking change, and you don't want the end user to even notice a difference, what do you do in these cases? And the solution is, we can interact with an object as an array whenever we need to. All you have to do is implement the ArrayAccess interface. And this is just a default PHP interface you can pull in. So if I add the method stubs here with phpStorm, here are all the methods we need to implement to make this work. offsetExists. This determines, does the offset exist? And it can be anything you want.

Defining the Message Class1:36

This determines, does the offset exist? And it can be anything you want. It could be a Collection that you're looking into. It could be a public property. Anything you want. So in our case, let's imagine this. If class Message, it's just kind of a dummy object. So if it has a title here, and it has a body, and let's just hard code these here to make it nice and easy. Okay, well if we want to interact with this instance, just like it was an array, where

Implementing ArrayAccess Methods1:55

it nice and easy. Okay, well if we want to interact with this instance, just like it was an array, where we can say message title, and it's still going to work just as if it was an array, well here's how we do that. Let's come back. offsetExists. Let's just return isset, and we're going to see if the property exists. So if offset is title, we're just going to say, well, does a title property exist on the class? All right, offsetGet, this is where we return it.

the class? All right, offsetGet, this is where we return it. So again, if you say messageTitle, we're now basically going to translate that to this title. All right, offsetSet. So this would be, if the user does something like, it's really not relevant to our needs, but nonetheless, if they were to say new equals thing, this method would then be triggered. So once again, we can just keep it simple and say this offset equals value. Finally, unset, that's kind of irrelevant to our needs, but if we wanted to implement that, we might unset it like this, or of course, you can just leave it unimplemented if you

Testing Array-Style Access2:49

Finally, unset, that's kind of irrelevant to our needs, but if we wanted to implement that, we might unset it like this, or of course, you can just leave it unimplemented if you want. Okay, so let's go ahead and format that, and believe it or not, we have everything we need at this point. So let's give it a shot. We're going to say $m equals a new Message, and let's just echo out the $title. Okay, we'll do this from the command line. So command line php example.php, and sure enough, we did get the $title, but we would expect that because we are referencing it as a property.

So command line php on example.php, and sure enough, we did get the title, but we would expect that because we are referencing it as a property. But now, if we want to reference it as an array so that we don't introduce a breaking change for the users, we're still going to get the exact same thing. And once again, that's specifically because we implemented the ArrayAccess interface. So if I were to remove that entirely and run it, of course, it's going to blow up. Cannot use object of type Message as array. But if we implement ArrayAccess, well, that changes everything, and now we can interact with it like it's an array. And we can even override it.

So in some ways, you can think of this as a nice stopgap until you get to maybe the next official release where you then deprecate this array syntax. Okay, so I hope it helps.

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