-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Export stdClass objects using (object) cast #2420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Did you think about adding I don't care how it's implemented, but 👍 |
I didn't! But that's an interesting idea, and we might want to also implement that. Using Adding I wonder if any userland code uses One argument against adding it to One benefit of |
See also: https://bugs.php.net/bug.php?id=48016 |
Is there an RFC for this? I don't forsee issues, but it's non-trivial enough to merit discussion IMO. |
I agree. If overwhelming consensus emerges from a discussion, it may be possible to skip the actual RFC. Discussion is where we should start ... |
I don't think it requires an RFC, a heads up on internals should be enough. |
@sgolemon @krakjoe @kelunik I've pinged internals here: http://news.php.net/php.internals/98521 |
What happens of some class extends stdclass? Without adding __set_state I imagine any class extending stdclass without defining __set_state is still broken. If we added __set_state though, it'd be fixed too. |
ext/standard/var.c
Outdated
smart_str_appendl(buf, "::__set_state(array(\n", 21); | ||
/* stdClass has no __set_state method, but can be casted to */ | ||
if (Z_OBJCE_P(struc) == zend_standard_class_def) { | ||
smart_str_appendl(buf, "(object)array(\n", 15); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if relevant, but PHP documentation uses space after cast, i.e. (object) array(...)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to read and I thought about commenting that, too. But on the other hand it saves bytes in storage that way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer how it looks without the space.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to change this bit prior to merging. We get PRs fixing the var_export style from time to time (you know, fixing broken/inconsistent indentation, using short array syntax, that kind of stuff). While we have to reject these for BC reasons, it would be preferable if we at least did not introduce unorthodox formatting for new functionality.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I decided to not be stubborn and accept the apparent overwhelming consensus here, and changed this when I rebased today. The manual is a particularly good argument.
CI's doing something weird here. The second commit is marked as failing, but the failing Travis build is for a different commit. |
@TazeTSchnitzel try to nudge Travis with an empty commit |
Given this is a new addition, and 5.3 is out of support, we could perhaps use short array syntax ( |
ext/intl/tests/dateformat_format.phpt failure looks legit. |
@TazeTSchnitzel From Travis docs:
That's why you're seeing a different commit ID. |
Oh, I'm silly. I fixed one |
f52e6f4
to
29a21b8
Compare
I completely forgot about this. I don't think there's been any opposition… do @remicollet @sgolemon have any interest in merging this for 7.2? Otherwise I'll put it in master. |
@smalyshev @cmb69 Since it's 7.3 soon and nobody has touched this for a year, do you have any interest in this change? |
@hikari-no-yume I certainly like this PR (and IMHO https://bugs.php.net/48016 is more bug than feature request). However, it's not the role of the RMs to decide what features go into a release. Since this discussion as well as the internals discussion showed some appriciation and no objections, I'm pragmatic: If nobody objects, I'll merge this PR into master on 2018-07-12, so that it goes into PHP 7.3. |
Sounds good. I should have merged this into master a while ago, to be honest. |
I would add a note in UPGRADING though. |
Sure. :) |
29a21b8
to
74c9275
Compare
I rebased it, squashed it, tested it still worked, changed the syntax used from |
74c9275
to
b934e62
Compare
Before this change, var_export()'s output for stdClass objects calls the non-existent stdClass::__set_state method, and is therefore useless. This commit makes var_export() output an (object) cast from an array instead, which when evaluated, will produce a stdClass object. Other classes see unchanged output.
b934e62
to
c7a37e1
Compare
smart_str_appendl(buf, "::__set_state(array(\n", 21); | ||
/* stdClass has no __set_state method, but can be casted to */ | ||
if (Z_OBJCE_P(struc) == zend_standard_class_def) { | ||
smart_str_appendl(buf, "(object) array(\n", 16); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hardest two things in computer science are, of course, off-by-one errors. This was briefly smart_str_appendl(buf, "(object) array(\n", 15);
and I was confused why there was no newline…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sizeof(…)-1
to the rescue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to do one better and use ZEND_STRL
, but I can't because smart_str_appendl
is a macro. I could use smart_str_appends
, but that it uses strlen
annoys me at some level, even though any reasonable compiler™ will do the right thing…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I was not aware of either! ZEND_STRL
is … fascinating! Anyway, smart_str_appends
might still be preferable here – after all, it's just about a potentially missing optimization, and I prefer clean code over any optimization, unless the latter proves to be worth the trouble. I'm fine with your current solution, though, since it doesn't make things worse.
Comment on behalf of cmb at php.net: Thanks! Applied via e4e9cd8. |
Before this change,
var_export()
's output forstdClass
objects calls the non-existentstdClass::__set_state
method, and is therefore useless.This commit makes
var_export()
output an(object)
cast from an array instead, which when evaluated, will produce a stdClass object. Other classes see unchanged output.