suchen

Heim  >  Fragen und Antworten  >  Hauptteil

Die Erstellung dynamischer Eigenschaften in PHP ist veraltet: Warnung

Ich sehe das immer häufiger, bin mir aber nicht sicher, was getan werden muss, um diese Warnung zu stoppen:

VERALTET: Dynamische Eigenschaften erstellen... VERALTET

Das ist meine Klasse:

class database {

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

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

So instanziiere ich es.

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

Das gibt mir zwei Botschaften:

VERALTET: Dynamische Eigenschaftendatenbank erstellen::$database Veraltet

VERALTET: Dynamische Eigenschaftendatenbank erstellen::$server Veraltet


P粉797855790P粉797855790408 Tage vor1018

Antworte allen(5)Ich werde antworten

  • P粉299174094

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

    该警告告诉您您尝试设置的属性未在类顶部列出

    当您运行此命令时:

    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',
    ));

    大致相当于这样:

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

    警告是类定义中没有行表明 $db->database$db->server 存在。

    目前,它们将动态创建为非类型化公共属性,但将来,您需要显式声明它们:

    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',
    ));

    在一些罕见的情况下,你实际上想说“这个类的属性是我决定在运行时添加的任何属性”;在这种情况下,您可以使用 #[AllowDynamicProperties] 属性,如下所示:

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

    Antwort
    0
  • 徐涛

    啊啊大师傅

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

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

    山东省滨州市***工资搭嘎发撒***哈

    Antwort
    0
  • P粉098979048

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

    因此警告来自添加动态类属性的构造函数。如果您不必动态且真实地传递这些字段,那么您似乎确实将简单的事情变得过于复杂,那么请像这样尝试。

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

    您需要动态参数有什么原因吗?您也可以这样做:

    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;
            }
        }
    }

    如果您提前添加参数,它们就不是动态的,您只是为已经存在的内容分配一个值。

    现在应该可以正常工作,不会出现任何警告。

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

    Antwort
    1
  • 徐涛

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

    阿帆VS工作细胞宣传部先擦VB

    Antwort
    0
  • StornierenAntwort