search

Home  >  Q&A  >  body text

Dynamic property creation in PHP is deprecated: warning

I'm seeing this happen more and more, but I'm not sure what I need to do to stop this warning:

DEPRECATED: Creating dynamic properties... DEPRECATED

This is my class:

class database {

    public $username = "root";
    public $password = "password";
    public $port = 3306;

    public function __construct($params = array())
    {
        foreach ($params as $key => $value)
        {
            $this->{$key} = $value;
        }
    }
}

This is how I instantiate it.

$db = new database(array(
    'database' => 'db_name',
    'server' => 'database.internal',
));

This gives me two messages:

DEPRECATED: Create dynamic properties database::$database Deprecated

DEPRECATED: Create dynamic properties database::$server Deprecated


P粉797855790P粉797855790405 days ago1015

reply all(5)I'll reply

  • P粉299174094

    P粉2991740942023-10-21 10:14:38

    This warning tells you that the property you are trying to set is not listed at the top of the class.

    When you run this command:

    class database {
    
        public $username = "root";
        public $password = "pasword";
        public $port = 3306;
    
        public function __construct($params = array())
        {
            foreach ($params as $key => $value)
            {
                $this->{$key} = $value;
            }
        }
    }
    
    $db = new database(array(
        'database' => 'db_name',
        'server' => 'database.internal',
    ));

    is roughly equivalent to this:

    class database {
    
        public $username = "root";
        public $password = "pasword";
        public $port = 3306;
    }
    
    $db = new database;
    $db->database = 'db_name';
    $db->server = 'database.internal';

    The warning is that there is no line in the class definition indicating that $db->database or $db->server exists.

    Currently, they are dynamically created as untyped public properties, but in the future, you will need to declare them explicitly:

    class database {
        public $database;
        public $server;
        public $username = "root";
        public $password = "pasword";
        public $port = 3306;
    
        public function __construct($params = array())
        {
            foreach ($params as $key => $value)
            {
                $this->{$key} = $value;
            }
        }
    }
    
    $db = new database(array(
        'database' => 'db_name',
        'server' => 'database.internal',
    ));

    In some rare cases you actually want to say "the properties of this class are whatever properties I decide to add at runtime"; in that case you can use #[AllowDynamicProperties] Attributes, as shown below:

    #[AllowDynamicProperties]
    class objectWithWhateverPropertiesIWant {
        public function __construct($params = array())
        {
            foreach ($params as $key => $value)
            {
                $this->{$key} = $value;
            }
        }
    }

    reply
    0
  • 徐涛

    Ah, master

    徐涛 · 2023-10-26 17:53:30
  • 徐涛

    徐涛2023-10-27 09:34:37

    Binzhou City, Shandong Province***The salary is so high that ***ha

    reply
    0
  • P粉098979048

    P粉0989790482023-10-21 10:03:08

    So the warning comes from the constructor adding a dynamic class attribute. It does seem like you are making something simple too complex if you don't have to pass the fields dynamically and authentically, so try something like this.

    class database {
    
        public $username = "root";
        public $password = "pasword";
        public $port = 3306;
        public $database = 'db_name';
        public $server = 'database.internal';
    }
    
    
    $db = new database();

    Is there any reason why you need dynamic parameters? You can also do this:

    class database {
    
        public $username = "root";
        public $password = "pasword";
        public $port = 3306;
        public $database;
        public $server;
    
        public function __construct($params = array())
        {
    
            foreach ($params as $key => $value)
            {
                $this->{$key} = $value;
            }
        }
    }

    If you add parameters ahead of time, they are not dynamic, you are just assigning a value to something that already exists.

    It should now work without any warnings.

    $db = new database(array(
        'database' => 'db_name',
        'server' => 'database.internal',
    ));

    reply
    1
  • 徐涛

    徐涛2023-10-26 17:55:40

    Afan VS Work Cell Propaganda Department first wipe VB

    reply
    0
  • Cancelreply