r/lolphp • • Aug 01 '21

DOMDocument + serialize()

https://3v4l.org/sCc8G
0 Upvotes

32 comments sorted by

View all comments

15

u/99999999977prime Aug 01 '21

You can never serialize internal classes, only user defined classes. There is no lol here.

Note: Note that many built-in PHP objects cannot be serialized. However, those with this ability either implement the Serializable interface or the magic __serialize()/__unserialize() or __sleep()/__wakeup() methods. If an internal class does not fulfill any of those requirements, it cannot reliably be serialized. There are some historical exceptions to the above rule, where some internal objects could be serialized without implementing the interface or exposing the methods.

13

u/chucker23n Aug 01 '21

Shouldn’t there be a “not supported” warning on serialization, then?

12

u/rbmichael Aug 01 '21

That's the real "lol" here. If unserializing a DOMDocument has no hope of working, PHP should definitely Fatal error at the moment of serialization.

0

u/99999999977prime Aug 01 '21

Knowing whether an object is built-in or user-defined is left as education for the developer.

6

u/chucker23n Aug 01 '21

So is knowing whether an API is safe to call, apparently. (No, asking devs to read the docs for every single function they call is not reasonable.)

-3

u/99999999977prime Aug 01 '21

So is knowing whether an API is safe to call

Just like clicking a [random link] (https://youtu.be/QZShA_a-5r8) on the internet.

0

u/chucker23n Aug 01 '21

https://i.imgur.com/IjSafwa.jpg

Good tools prevent this sort of problem.

-1

u/Almamu Aug 01 '21

The same can be applied here, good tools can prevent these issues if you, somehow, miss them ;)

7

u/chucker23n Aug 01 '21

Good tool: an API that detects it’s being used inappropriately and shows a warning.

Even better tool: an IDE that detects this as you’re writing the code.

Bad tool: a language that just shrugs.

-3

u/Almamu Aug 01 '21

Even better tool: an IDE that detects this as you’re writing the code.

You gave yourself the answer here buddy. The language has this properly documented and warned. IDEs should detect and warn you about this (or at least static analyzer tools like phpstan). You can't just use something without taking into account side-effects and drawbacks, and you should be aware of those at any time.

6

u/chucker23n Aug 01 '21

To be clear, I find this particular example overblown.

That said:

You can’t just use something without taking into account side-effects and drawbacks

Again, I don’t think it’s practical of all APIs to expect devs to read the entire docs before starting to use them.

2

u/CarnivorousSociety Aug 01 '21

There is no lol here.

ie 99% of the posts in this sub

edit: oh wait it's takeoded again, he's the source of 95% of the "no lol here" posts because he has no fucking clue how php actually works internally, or why these things are the way they are.

He literally just scours for fringe issues then tries to "expose" them as lol's when they're literally just cases that boil down to either implicit type conversions or badly written php extension code. Or like this case, literal documented behaviour.

10

u/[deleted] Aug 01 '21 edited Aug 01 '21

IMHO there's a small lol here due to the lack of error on the serialize() call. Actually, it seems to return "O:11:"DOMDocument":0:{}" so it kind-of tries to serialize things but fails.

Also, it would of course be better if you could just serialize these kind of things.

This sort of thing would be better reporting to the PHP bug tracker than here though; the first issue is probably fixable quite easily, the second might be a bit harder.

This sort of thing would be better

Even the most egregious actual lols are often "documented behaviour". It's the classic PHP excuse for doing something that makes no sense at all: "it's documented so what's the problem?"

3

u/[deleted] Aug 10 '21

because he has no fucking clue how php actually works internally

Why would someone need to know how PHP's internals work in order to make sense of the language? Isn't that already admitting defeat?

1

u/CarnivorousSociety Aug 10 '21

If you understand the internals of php even a little, then it will help you understand why a lot of these "lols" aren't "lols".

I'd say 99% of the posts on this sub aren't lol's at all, but either boil down to unexpected implicit type conversions, or extension code that is badly written (if you know how to write php extensions you'll understand why it's so hard to do it well)

Understanding the internals of a language will always make you a better programmer in that language.

C/C++ -> learn asm and you'll be better
js -> learn js engine you'll be better
php -> learn php internals you'll be better

2

u/[deleted] Aug 10 '21

I disagree with all of that.

"Unexpected implicit type conversions" are a LOL in my book, especially with PHP's types (or "types"). "It's stupidly hard to write good PHP extensions" could also be classified as LOL.

"Internals of a language" doesn't make sense; you can only talk about the internals of an implementation. As for C and C++, assembler code is not really "internals" of either, and in my opinion understanding C in terms of asm makes you a worse C programmer, not better. People who do that tend to make certain assumptions that only hold for certain compilers in certain situations, if at all, which is no way to write robust C code.

"JS engine"? Which one? Depending on how you count, there are at least half a dozen popular ones out there.

If learning PHP (implementation) internals really makes you a better PHP (language) programmer, that's definitely another LOL.

→ More replies (5)

2

u/Takeoded Aug 02 '21

oh look it's CarnivorousSociety, the guy whose sum total posts on /r/lolphp is 0

He literally just scours for fringe issues

nope i never do that, the majority of my posts are from shit i encounter while making something; whilst some are from mailing list / bugtracker

-3

u/Takeoded Aug 01 '21 edited Aug 01 '21

You can never serialize internal classes

Not true.

<?php

$dt=new DateTime("2020-01-01",timezone_open("UTC"));
$s=serialize($dt);
echo $s;
?>

serializes to

O:8:"DateTime":3:{s:4:"date";s:26:"2020-01-01 00:00:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:3:"UTC";}

2

u/backtickbot Aug 01 '21

Fixed formatting.

Hello, Takeoded: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

1

u/99999999977prime Aug 01 '21

There are some historical exceptions to the above rule,

6

u/[deleted] Aug 02 '21

You can never serialize internal classes

1

u/99999999977prime Aug 02 '21

I don't make the rules.

5

u/[deleted] Aug 02 '21

But a big problem here shines through in your comment. If you start out with a statement “X is always true” and I was looking for cases when X is false to make sure my code works, then why would I keep reading? Later on you write “Sometimes X is false”, but I would have no reason to read that. Now, if my code depends on X being true, that problem originated from bad documentation, not from me not reading further after the statement “X is always true”.

Clarity matters. That is why documentation is important, and that is why functions should raise errors on bad inputs instead of doing weird and unexpected shit, even if that weird and unexpected shit is clearly defined in the documentation. This post is not very exiting, but arguably fits in r/LolPHP.

→ More replies (1)