- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
PHP. TRWTF.
Admin
Transformation between (relevant) types by serialization and deserialization is actually quite common and useful. In fact many mainstream, well-designed systems rely on exactly this mechanism...
Admin
Can someone explain that one please
Admin
The serialize function transforms any variable (including objects) into a string representation. This string may be unserialized to obtain the same variable/object again.
O:7:"MyClass":0:{}
When serializing an object, the name of the class becomes part of the serialized string. So if you serialize, str_replace() the class name and then unserialize... VOILA! you get an object of a different type!!
Admin
it's not a wtf in itself, much more the symptom of a deeper, much worst wtf somewhere in the code base and a demented coder in the office.
Admin
I feel sick.
A truly 'golden' WTF. Thanks, I guess?
Admin
my eyes! they hurt!
this is wrong on so many levels....
Admin
Please don't blame this on PHP. Stupid coders do stupid shit in any language. -_-;;
Admin
Well, that's the most unholy bit of coding I've seen on this site in a long, long time. I mean, I know the OO capabilities of PHP are lacking compared to most other languages, but this... I mean, damn.
It's the combination of technical proficiency needed to even make that work and complete lack of understanding at how messed up this will be for maintenance coding this will be that takes the cake.
Admin
That just shows what a hack PHP is.
Real languages have a defined API, and make that much easier.
Admin
Well, real type conversion is only for real programmers using real programming languages...
Admin
Funny... He could have just returned the unserialized object instead of molesting the input.
Admin
I don't get at all what this might be useful for ...
Admin
I really wonder what happens when you try this with incompatible classes. Presumably the deserializer will block such operations, but then again, it is generally best not to presume too much with PHP.
Admin
Don't worry, it does exactly what you expect from PHP:
Admin
This code has made me literally feel ill.
I always thought when people say that it was just hyperbole, but it turns out that it is actually possible to feel sick just from looking at egregiously awful code.
Admin
Wells yes but usually you do a little more than applying a regex to the string that tells the interpreter what type of object its going to unserialize. That and they objects/types need to be compatible, I assume that is what you mean by 'relevant'.
This code would be perfectly happy to try and turn a duck into a car. It would be nice if it had duck-type style checks or something in it that would produce a useful error if you try and transmogrify something that won't work. I would guess there was a need transform between two very specific types that lead to this 'solution' and it would be nice if the code checked that it really was getting them as input and raised an understandable exception when something else is passed.
Good luck to whoever gets to maintain this codebase.
Admin
Of course, smalltalk had this feature years ago with become http://gbracha.blogspot.co.uk/2009/07/miracle-of-become.html while open to abuse, it can also allow for some interesting solutions to things like the state pattern.
Admin
Yes, and in Smalltalk you can change the class of every object easily with #changeClassToThatOf:
http://www.cincomsmalltalk.com/userblogs/mls/blogComments?entry=3315628761
Admin
Maybe that's the trick to get a floating car:-)
Admin
Ths can be useful in certain cases. For example, when working with web services, the PHP implementation will return a generic object for you with all the fields of the class that you were trying to get but none of the methods as PHP doesn't know what class the object should be. If you want to actually convert the object into an instance of that class to be able to call methods of that class, you would need to do a hack like this.
Admin
Or you can use reflection. Even PHP has reflection support.
Admin
Looks like a smart answer to the wrong problem!
Admin
Why do people blame bad programming in a language, for example PHP, on the language itsself? Do we also blame bad grammar use on English?
Admin
Ok, let me see if I got this right... is he shoehorning a new type to existing data by replacing its class name?
Evil. In its purest form.
BTW... Brad, can you post his Linked In profile? ...just curiosity...
Admin
I hear a lot how this or that language is bad. Could somebody give me a list of languages which are good?
Admin
We can easily improve the function and allow arbitrary classnames. Also i added a boolean to control if we should try to include classes beforehand.
public static function changeClass(&$object,$newClassName, $boolTryToLoadClasses) { if (!class_exists($newClassName,$boolTryToLoadClasses)) { eval "class {$newClassName} {}"; } $object = unserialize(preg_replace("/^O:[0-9]+:"[^"]+":/i", "O:" . strlen($newClassName) . ":"" .$newClassName."":", serialize($object)));
} Although it does not shuffle, i have adapted the function into the gizmore framework ;)
Consequat: This word is self explaining.
Admin
So verbose and awkward. In Python it's just:
(not that you should actually do that unless you have a damn good reason)
Admin
"There are only two kinds of languages: the ones people complain about and the ones nobody uses." - Bjarne Stroustrup
Admin
Common - You're having a fucking laugh aren't you?
Admin
Couldn't you pretty much do this in any language though? Say C# you serialize an object. Then create a deserializer and pass it a type that looks just the same serialized as the other except with different methods. The real WTF would be if PHP doesn't have any other method to case objects (which might be the case I don't know never played with it) but I mean you are expressing your intent to convert from one type to another albeit indirectly. If you don't like what you get than that is your fault.
Admin
Well...
Admin
I think it should have been named blind duck.
Admin
If you're too lazy to setf:
Use case:
Output:
Returns final value of obj:
shudders
Admin
Sure, why not? English has a very complicated grammar and no consistent set of pronunciation rules. It's basically a hodgepodge of features stolen from other languages, hastily grafted together with little thought to consistency, that is widely used due to familiarity and network effects.
That's how languages work.
Admin
I had to do this in Java when the names of XML exported classes had to be changed (including the package name). This was required to provide for backward compatibility. Usually, backward compat is less hard to do - the deserializer just hast to have access to stub methods providing the old interface, but once the name of a class has to change (because code would be really ugly otherwise or the old class structure was a WTF - as was the case in my example) it might become dirty. So: This might be a WTF, but it might also be the right solution for certain use-cases. Just because you never came across it yourself does not make it a WTF.
Admin
Which, BTW, holds for many of the articles at TDWTF.
Admin
So, just like PHP, eh?
Admin
True enough, but PHP is the single most efficient productivity tool ever devised for the writing of shitty code. (An honorable mention goes to VB, of course).
Admin
In C, C++, C#, Java, etc. (even SQL) you can just use a cast to change the type. Much simpler.
Admin
I guess cloning the object was out of the question... There can't be any actual use for this ever or maybe I just lack the imagination that this dev had... It probably opens a portal to Cthulhu's beach house in the Hamptons.
Admin
Admin
Admin
ERROR: NO RECORDS FOUND.
Admin
Admin
I'd disagree. English grammar is one of the simpler ones. It may have a couple of rough edge cases, but mostly English grammar is dead simple.
The pronunciation of words varies from place to place, but this is the case with any language that is spoken among geographically divers people which do not all communicate each and every day with each other. English suffers from this a lot since it's probably the most geographically diverse language to date. Since there's only one, no, sorry, two spelling systems, you're bound to come across inconsistencies.
Name one natural language that does not fit this definition.
I find English pretty convenient due to its very rich vocabulary and very flexible grammar rules. Compared to the two other languages I speak (Japanese and German), it's mighty simple. Japanese can compete with it in terms of flexible and simple grammar, but ease of use and expressiveness is very high up there in English.
Admin
"{Your technology I don't like}. TRWTF." .... is TRWTF. Please have something to add when you comment.
Worst ... comment ... ever. Thank you.
Admin
So this is what the PHP equivalent of C++’s reinterpret_cast<> looks like.
Admin
For instance, it only works if duck.weight == car.weight, and CanBuildBridge(duck) returns true.
Admin
Wow! Well, ...
no.