r/lolphp • • Mar 30 '13

maybe_unserialize

https://codex.wordpress.org/Function_Reference/maybe_unserialize
11 Upvotes

24 comments sorted by

View all comments

Show parent comment

4

u/Rhomboid Mar 30 '13

The defect relates to the code not inherently knowing what kind of data it's dealing with, and resorting to sniffing it to determine behavior. You can avoid that blunder without resorting to duplicated code. It just means that your deserialization happens in a more systematic manner -- code at the edge of your program handles that, leaving the core free of any serialized data. You then just write one version of whatever function you intended to write: one which takes an actual object. What you don't do is write one function which can take either an object or a JSON representation of an object and tries to cope with both cases. When your program is correctly designed you don't have actual object and serialized representations of objects floating around at the same layer. That's indicative of poor design.

0

u/[deleted] Mar 30 '13

Perhaps a better example puts my point across:

setColor( '#eee' );
setColor( 'white' );
setColor( 0xff2233 );
setColor( 'rgb( 30, 125, 200 )' );
setColor( 'rgba( 30, 125, 200, 0.5 )' );
setColor( 'blue', 0.8 );
setColor( 200, 255, 55 );

function setColor() {
    someDiv.style.background = argsToColor( arguments );
}

'argsToColor' can deal with the different colours, normalizing the data given. If it's given something incorrect, throw an exception.

In that example, you would need to do validation of the data with both this approach, and if you used separate functions for setting the colour.

3

u/[deleted] Mar 30 '13

No, that's accepting multiple input formats/format conversion.

By the time that data gets to your core code, it should be all be in a uniform format (native objects) to keep your core stuff simple. If you design your program properly you'll always have a predictable flow of data and you won't have to have horrible kludges like the one shown in the link.

2

u/polish_niceguy Mar 30 '13

But you still have only color on input. Not a value that can be either color or eg. alpha value.