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

Dynamic properties will be deprecated in PHP8.2 #2367

Open
deg-pl opened this issue Oct 4, 2022 · 1 comment
Open

Dynamic properties will be deprecated in PHP8.2 #2367

deg-pl opened this issue Oct 4, 2022 · 1 comment

Comments

@deg-pl
Copy link
Member

deg-pl commented Oct 4, 2022

Dynamic class properties will be deprecated in PHP8.2 and will thow an ErrorException in PHP 9. For example below code will throw an error:

class Post
{
    public string $title;
}

// …

$post->name = 'Name';

We use dynamic properties in our code. Main in View and (nearly?) all templates.

But classes implementing __get and __set will still work as intended:

class Post
{
    private array $properties = [];
    
    public function __set(string $name, mixed $value): void
    {
        $this->properties[$name] = $value;
    }
}

// …

$post->name = 'Name';

Maybe this is simple/temporary solution?

@deg-pl deg-pl changed the title Deprecate dynamic properties in PHP8.2 Dynamic properties deprecated in PHP8.2 Oct 4, 2022
@deg-pl deg-pl changed the title Dynamic properties deprecated in PHP8.2 Dynamic properties will be deprecated in PHP8.2 Oct 4, 2022
@jrmajor
Copy link
Contributor

jrmajor commented Oct 4, 2022

Maybe this is simple/temporary solution?

The easiest solution is to add #[AllowDynamicProperties] attribute, like so:

#[AllowDynamicProperties]
class Post
{
    public string $title;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants