If you work with object-oriented PHP in WordPress and you’re building out various models that fit your web applications, the odds are that you’re going to deal with retrieving serialized versions of those models at some point during a program’s execution.
Here’s the thing, though: Sometimes, that unserialized data come back as standard PHP classes. This means that if you inspect the type (through various debugging tools), you’re going to see they are the type of stdClass.
If you’ve been properly building your models those, your code is going to have functions that the stdClass does not, and you’re going to want to call on them.
Further, you can’t simply cast them from one type to another like you can with native types (such as strings, integers, and so on). In situations like that, you need to be able to cast a PHP standard class to a specific type.
And here’s a function that will help you do just that.
Cast a PHP a Standard Class to a Specific Type
For this example, assume the following:
- I have a class that’s namespaced as Acme\Model\Product.
- It, at some point, is saved to the WordPress database but when retrieved is set as an instance of stdClass.
- I need the unserialized version of the object to be that of the Product.
To that end, I have the following function available that I drop into projects as I need it:
Sure, the function is commented with as much detail as I can provide, but there are a few things I can explain a bit further in the context of a post than I can in the context of a code comment.
Understanding the Code
First, it’s important to make sure you understand the following PHP functions (all of which are well-defined in the PHP manual):
- unserialize. Creates a PHP value from a stored representation.
- sprintf. Return a formatted string
- strlen. Get string length
- strstr. Find the first occurrence of a string.
- serialize. Generates a storable representation of a value.
So, yes, the incoming instance of the class is retrieved and then cast as the specified type but how do the above functions play a role in this? It has to do with how a class is serialized into the WordPress database.
Take for example the following string:
I know – it’s not exactly a pleasure to review, but it’s exactly how WordPress serializes an object. Further, when it’s retrieved from the database, it’s done so and then returned as a stdClass instance, not as an instance of the type it was before it was saved.
That’s where the above function comes into play. To restore it to its deal type, you’ll want to cast it as such. And to do that, you can simply do the following:
Note that I’m retrieving an object that’s been serialized to the options table. I’m not making a case as to if you should do this or not, this is for example purposes.
Secondly, note that I’m calling cast on an instance of $this so $this could be an instance of a class or it could be a method in the base class. It doesn’t matter (as long as the latter is marked as protected).
From there, you now have an instance of the class that you originally saved with all of the information available to you as it were when you first saved it.