8000 Never rely on dynamic properties by nicolas-grekas · Pull Request #44037 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Never rely on dynamic properties #44037

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

Merged
merged 1 commit into from
Nov 15, 2021
Merged

Conversation

nicolas-grekas
Copy link
Member
Q A
Branch? 4.4
Bug fix? no
New feature? no
Deprecations? no
Tickets -
License MIT
Doc PR -

As described in https://wiki.php.net/rfc/deprecate_dynamic_properties

@carsonbot carsonbot added this to the 4.4 milestone Nov 13, 2021
@nicolas-grekas nicolas-grekas force-pushed the no-dyn-prop branch 4 times, most recently from 0d01bd6 to 4de0371 Compare November 13, 2021 16:45
@nicolas-grekas
Copy link
Member Author

(PR is ready)

@@ -18,6 +18,9 @@
*/
class SMimePart extends AbstractPart
{
/** @internal */
public $_headers;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could even be private

Copy link
Member Author
@nicolas-grekas nicolas-grekas Nov 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made it protected and all similar ones too.
I prefer to not make them private to be extra sure not to break inheritance.
@internal+protected looks safe enough to me.

@chalasr
Copy link
Member
chalasr commented Nov 15, 2021

build failures seems related

@nicolas-grekas
Copy link
Member Author

Failures are not related to me. They happen because 3.4 is checked out and fails in flipped tests.

@chalasr
Copy link
Member
chalasr commented Nov 15, 2021

Thank you @nicolas-grekas.

@jmleroux
Copy link

Hello guys,

We have an issue with this PR and our usage of PhpSpec when asserting for Exception (using the shouldThrow matcher)

For instance, we are using it on an assertion like

$this->shouldThrow(Symfony\Component\Security\Core\Exception\AuthenticationException::class`)

The PhpSpec matcher will then iterate on the reflected Exception class properties to compare them, but willl crash here because the property does not exists anymore because it was unset in the constructor.

Do you have an idea how we should deal with this issue?

Regards,
JM

@jmleroux
Copy link
jmleroux commented Nov 22, 2021 8000

I think it can create issues with every tool using reflection.

@nicolas-grekas
Copy link
Member Author

@jmleroux thanks for the feedback. From what you describe this looks like an issue with the tool, that doesn't properly use reflection since it doesn't (yet) know how to deal with a situation that can exist with any class. Isn't it?

@jmleroux
Copy link

From what you describe this looks like an issue with the tool, that doesn't properly use reflection since it doesn't (yet) know how to deal with a situation that can exist with any class. Isn't it?

I could agree 🙂 but it is also very unusual to unset a property as the first constructor action, isn't it?

To be honest, I didn't understand this unset at first sight. After thinking a bit, I think that you wanted to "mimic" the dynamic property behavior when this property does not exists at instantiation, right?
But I still find it weird with the use of refection class, because the reflection will still see this property like a regular one, not a dynamic one.

@nicolas-grekas
Copy link
8000
Member Author
nicolas-grekas commented Nov 22, 2021

I understand. It's still something that is legal and has a clear purpose in PHP.
See https://wiki.php.net/rfc/deprecate_dynamic_properties for why this is needed.
Since PHP 7.4, ReflectionProperty::isInitialized() can be used to deal with the situation.
If you need to support older PHP versions, you might want to use the (array) casting operator, as done in this PR.

@jmleroux
Copy link

I didn't see this use case was documented in the "Backward Incompatible Changes". 😶

Thank you for quick and clear answers @nicolas-grekas !

mbabker added a commit to mbabker/JWTRefreshTokenBundle that referenced this pull request Dec 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants
0