This episode is for members only

Sign up to access "PHP Enums" right now.

Get started
Already a member? Sign in to continue
Playing
04. Serialisation and deserialisation

Transcript

00:00
Technically, we've already looked at serializing an enum
00:03
by grabbing the value from each of the cases that we've chosen here. But what about deserializing them? That basically means that when we put this raw value
00:12
into our database, when we get this value out of our database, we want this to be represented as an enum. Now, at the moment, that might make not much sense. We might just want the value to output in our UI
00:24
or wrap in an if statement to show some sort of message. But actually grabbing back an enum from the database or deserializing it can have massive advantages, and we're going to look at why very shortly.
00:37
Okay, so let's imagine that we have a model class in here. Let's just get rid of all of this here. So let's just create out a book class, and let's just imagine that this is the result
00:46
of making a query to the database, and again, using your favorite framework or ORM, getting back these attributes within this model. So let's represent this by just really, really simply
00:57
creating out an attributes array inside of here. So this is the data that's coming back from the database. So let's just convert these to numbers, I think, just to make this a little bit easier to work with.
01:10
So imagine that we're storing these as numbers in the database, and the status of a book is stored in the database, and we get this back as one. So if we just var dump out on a new book,
01:23
imagining we're getting that back from the database, and we actually fix the fact that we've changed these to integers, we should see that we get the status back of one.
01:33
Now, that's not very helpful. Again, you could wrap this in an if statement if you wanted to. If it was a string, it would be a little bit easier
01:39
to work with, but that's not very helpful. So how would we grab this out here? Well, we'd probably have a much nicer way of doing it than accessing some sort of attributes array,
01:49
but we would probably grab the attributes out here, and then go ahead and grab the status like this, and let's just wrap this here, and that gives us back one.
02:01
So how do we deserialize this to grab an enum back from this particular status? Well, let's just copy this line down. What we can actually do is we can grab the value
02:12
that we wanna get back, and let's just assign this book up here just to make this a little bit tidier, like so, and we can use book status,
02:26
and then we can say from, and then we can pass the value in. So if I were to var dump out on book status from, have a think about what you might get back
02:35
when we dump this, considering that this is the case we want to read. Let's run our code, and there we go. We get an enum back with that particular case in there.
02:46
Now, again, at the moment, that might not make much sense, because what would we then do with this enum object? We're gonna look at that in the next episode, but let's just look at a couple of things
02:57
around this first of all. So what we would actually do is from here in real life, grab that status back from the book. So if we just go ahead and run this,
03:07
we've got want to read. If this was stored in the database as two, and we were grabbing that enum out, we would have reading and so on and so forth.
03:14
But what if this doesn't exist? So let's just say we had this value in here as five, and we'd stored that previously, but this has changed,
03:21
or something had been manually inserted into the database, or this value was null, so it hadn't been stored. Let's just run our code again, and you can see that we get an error here.
03:30
So five is not a valid backing value for the enum of book status. So that kind of makes sense. What we can actually do is we can gracefully handle this,
03:38
and we can use try from, and that's gonna go ahead and return to us a null value if it's not found. So this is a safer option
03:45
if you have something in your database that doesn't quite exist. And you can see that we just get a null value back from there instead.
03:51
So try from is really handy, so we don't throw an error if this particular status doesn't exist. Okay, so before we go over and look
03:59
at why we would want to deserialize an enum, let's just take another really, really quick look at something else in terms of deserialization so it's in the back of our mind,
04:08
and that is API responses. So let's say that we have this book status just here, and we want to return some sort of JSON response within an API.
04:18
Well, we'd have a book title in here, so let's just, again, just pop this in here, and we'd have a status in here of the enum object that we get back based on the status
04:28
that we get back from the database. Now, if we go ahead and JSON encode this and dump it out, let's just guess what might happen. So let's say JSON encode and response.
04:38
So obviously, an object isn't gonna be within a JSON response, so let's just see what we get instead. So let's just go ahead and run this,
04:46
and you can see here we get a status of null, of course, because that doesn't exist. Let's just switch it back to one for now so we can see something useful,
04:53
and let's just run this. So what's happened is when PHP has deserialized this and JSON encoded that enum, it's given us the actual value.
05:02
So if you wanted the actual value in your API response, you would just go ahead and deserialize your enum, and then go ahead and pass it directly through to a JSON response.
05:13
In this case, of course, you might be thinking, well, I could just go ahead and pass the actual status itself back, and I would get exactly the same result,
05:21
and that is true, but deserialization is really helpful because in the next episode, we're gonna look at adding a method to this book status
05:33
so we can get a little bit more useful information back from this status. So that's pretty much all we really need to know for deserialization.
05:43
If that hasn't made sense in the next episode, we're gonna look at, like I said, adding a method to our book status object so we can grab some pretty much more useful information
05:53
back from this, and then you can come back and review this episode to piece this together. So let's jump over and look at that next.
7 episodes 31 mins

Overview

Enums represent a typed set of possible values in PHP. For example, if you're storing a Book that can have a status of want to read, reading or read, an Enum will hold these values in one place that you can reference anywhere.

Prior to PHP 8.1 (when Enums were added), you'd have to work with a simple class with constants. While that works fine, it doesn't provide much power under the hood.

In this course, we'll look at the power of Enums and how they can drastically simplify your code.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!