Skip to content
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

PHP 8.2 - Deprecate dynamic properties #307

Closed
icyz opened this issue Jan 12, 2023 · 23 comments
Closed

PHP 8.2 - Deprecate dynamic properties #307

icyz opened this issue Jan 12, 2023 · 23 comments
Labels
bug Something isn't working

Comments

@icyz
Copy link

icyz commented Jan 12, 2023

hello everyone,
is it planned to face the problem of "Creation of dynamic property"? I don't know, but this will be a bit painful.

Deprecated: Creation of dynamic property Zend_View::$var is deprecated

@Hikariii
Copy link

I think this is impossible to fix. Zend_View created properties dynamically. This looks like zend_view is not usable like this in php8.2

@glensc
Copy link
Collaborator

glensc commented Jan 13, 2023

@Hikariii
Copy link

The #[AllowDynamicProperties] attribute would be a nice solution here indeed.

@Hikariii
Copy link

@glensc
Copy link
Collaborator

glensc commented Jan 15, 2023

The setAttrib could be rewritten to store data in $this->data container, but it requires the properties are not actually accessed.

@icyz
Copy link
Author

icyz commented Jan 16, 2023

you can still use the attribute:

Would this be a temporary solution? or permanent?
I think the #[AllowDynamicProperties] attribute won't fix the problem, just postpone it...

@Hikariii
Copy link

Reading up on https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.dynamic-properties it looks like implementing a __set() and __get() method is more future-proof.

@Hikariii
Copy link

On the other hand, not using this repository is even way more future-proof

@icyz
Copy link
Author

icyz commented Jan 16, 2023

On the other hand, not using this repository is even way more future-proof

but we love this repository, so we have to do the best we can.

@glensc
Copy link
Collaborator

glensc commented Jan 16, 2023

You can also extend stdClass which is written on those links that it already has #[AllowDynamicProperties] set automatically. I don't think either of them is going away. not the attribute nor stdClass. I prefer #[AllowDynamicProperties] because it's purpose is distinct, extending stdCass may introduce some other unwanted funny bugs.

@Hikariii
Copy link

I see #[AllowDynamicProperties] has already been used. Hikariii@f6f029c
As I see it this changes the composer requirement to "php": ">=8.0" because php7 does not know about attributes. So it looks like an inconsistency has gotten into the codebase, or the composer requirements are not updated.

@glensc
Copy link
Collaborator

glensc commented Jan 17, 2023

no. #[AllowDynamicProperties] doesn't change requirements, because php7 doesn't understand attributes neither does it complain about dynamic properties.

@Hikariii
Copy link

ok that's good then adding the attribute should be the way to go

@vijaygarg-ahead
Copy link

Can anyone give a permanent solution to this issue?

@VentyCZ
Copy link

VentyCZ commented Jan 23, 2023

This should have been fixed in 1.22.0 if I'm not mistaken. Zend_View_Abstract contains changes needed, see #261.

@thijsvdanker
Copy link

The fixes in #261, specifically commit 34331f7, breaks our application.

We have wrapped Laravel around our ZF application and run the Blade compiler after ZF has done it's job.

If #[AllowDynamicProperties] keeps the code before 34331f7 running that would be great for us 😬

@glensc
Copy link
Collaborator

glensc commented Feb 4, 2023

@thijsvdanker you need to provide a standalone reproducer for your report to be useful.

@thijsvdanker
Copy link

@glensc I understand.
I don't really think my usecase is technically sound enough to be part of the discussion. I just wanted to put forward that if all else equal, the "non-breaking" change would have my preference.

@nunnrlc
Copy link

nunnrlc commented Feb 14, 2023

The fixes in #261, specifically commit 34331f7, breaks our application.

We have wrapped Laravel around our ZF application and run the Blade compiler after ZF has done it's job.

If #[AllowDynamicProperties] keeps the code before 34331f7 running that would be great for us 😬

Same here, broken after putting the view dynamic properties into private $_data = []; in library/Zend/View/Abstract.php
Reason is use of property_exists() in our codebase. Luckily only once, and caught in testing, so not a big deal here!

Actually, I don't mind the zf1-future change, but just to help others I'll explain the fix...

In our codebase we had property_exists() to test for a view variable in a phtml template

if (property_exists($this, 'someViewProperty')) { // $this is the view instance.
  ...
}

Well that doesn't work now the dynamic properties are stored in a __get/__set array after commit 34331f7

Easily changed our code to fix with isset():

if (isset($this->someViewProperty)) { // $this is the view instance.
  ...
}

So maybe a mistake to ever use property_exists rather than isset. The isset function plays nicely with __get/__set, but property_exists is strictly honest about "real" properties. A PHP gotcha I guess.

@holtkamp
Copy link

holtkamp commented Jun 7, 2023

When using the ContextSwitch ActionHelper, a 'contextKey' is used which defaults to contexts:

protected $_contextKey = 'contexts';

No setters are available to influence the value of the 'contextKey'.

In various parts of the class it then checks whether this property is set on the Controller and if not, will set it:

$contextKey = $this->_contextKey;
if (!isset($controller->$contextKey)) {
$controller->$contextKey = [];
}

I circumvented / resolved this specific deprecation by adding public array $contexts property in my own base Controller with a comment about why that property exists.

But as suggested in #307 (comment) #[AllowDynamicProperties] could be added to https://github.com/Shardj/zf1-future/blob/7fa9c5f50f0ce8ceecafb13e107eebe4c3d04051/library/Zend/Controller/Action.php

@develart-projects
Copy link
Collaborator

Feel free to create an PR for that and add #[AllowDynamicProperties]. It makes no sense to rework it.

@develart-projects develart-projects added the bug Something isn't working label Aug 9, 2023
@hungtrinh
Copy link

This issue resolved here: 34331f7 by PR #268

@develart-projects
Copy link
Collaborator

@hungtrinh well, using private properties should be forbidden, unless properly justified. Completely breaking possible inheritance. Anyway, thanks for pointing that out, we are done here then :)

falkenhawk added a commit to zf1s/zf1 that referenced this issue Jan 3, 2024
It looks like it's safe to use the `#[AllowDynamicProperties]` attribute, and it should still be valid for php 9.0 and not throw an error, contrary to what I understood at first.

As per RFC:
https://wiki.php.net/rfc/deprecate_dynamic_properties#proposal
"The creation of dynamic properties on classes that aren't marked with the #[AllowDynamicProperties] attribute is deprecated in PHP 8.2 and becomes an Error exception in PHP 9.0."

Related issues from zf1-future where they went with refactoring those classes to use a defined property to store all previously dynamic props in an array, which introduced breaking changes:
Shardj/zf1-future#307 (comment)
Shardj/zf1-future#261
Shardj/zf1-future#268
Shardj/zf1-future#328
Shardj/zf1-future#329
falkenhawk added a commit to zf1s/zend-form that referenced this issue Jan 4, 2024
It looks like it's safe to use the `#[AllowDynamicProperties]` attribute, and it should still be valid for php 9.0 and not throw an error, contrary to what I understood at first.

As per RFC:
https://wiki.php.net/rfc/deprecate_dynamic_properties#proposal
"The creation of dynamic properties on classes that aren't marked with the #[AllowDynamicProperties] attribute is deprecated in PHP 8.2 and becomes an Error exception in PHP 9.0."

Related issues from zf1-future where they went with refactoring those classes to use a defined property to store all previously dynamic props in an array, which introduced breaking changes:
Shardj/zf1-future#307 (comment)
Shardj/zf1-future#261
Shardj/zf1-future#268
Shardj/zf1-future#328
Shardj/zf1-future#329
falkenhawk added a commit to zf1s/zend-view that referenced this issue Jan 4, 2024
It looks like it's safe to use the `#[AllowDynamicProperties]` attribute, and it should still be valid for php 9.0 and not throw an error, contrary to what I understood at first.

As per RFC:
https://wiki.php.net/rfc/deprecate_dynamic_properties#proposal
"The creation of dynamic properties on classes that aren't marked with the #[AllowDynamicProperties] attribute is deprecated in PHP 8.2 and becomes an Error exception in PHP 9.0."

Related issues from zf1-future where they went with refactoring those classes to use a defined property to store all previously dynamic props in an array, which introduced breaking changes:
Shardj/zf1-future#307 (comment)
Shardj/zf1-future#261
Shardj/zf1-future#268
Shardj/zf1-future#328
Shardj/zf1-future#329
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

10 participants