The warning is telling you that there is a property you are trying to set which isn't declared in the class or any of its parents.
When you run this:
class database {
public $username = "root";
public $password = "pasword";
public $port = 3306;
public function __construct($params = array())
{
foreach ($params as
value)
{
$this->{$key} = $value;
}
}
}
$db = new database(array(
'database' => 'db_name',
'server' => 'database.internal',
));
It 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 saying that $db->database or $db->server exist.
For now, they will be dynamically created as untyped public properties, but in 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
value)
{
$this->{$key} = $value;
}
}
}
$db = new database(array(
'database' => 'db_name',
'server' => 'database.internal',
));
In some rare situations, you actually want to say "the properties of this class are whatever I decide to add at run-time"; in that case, you can use the #[AllowDynamicProperties] attribute, like this:
#[\AllowDynamicProperties]
class objectWithWhateverPropertiesIWant {
public function __construct($params = array())
{
foreach ($params as
value)
{
$this->{$key} = $value;
}
}
}
Answer from IMSoP on Stack OverflowThe warning is telling you that there is a property you are trying to set which isn't declared in the class or any of its parents.
When you run this:
class database {
public $username = "root";
public $password = "pasword";
public $port = 3306;
public function __construct($params = array())
{
foreach ($params as
value)
{
$this->{$key} = $value;
}
}
}
$db = new database(array(
'database' => 'db_name',
'server' => 'database.internal',
));
It 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 saying that $db->database or $db->server exist.
For now, they will be dynamically created as untyped public properties, but in 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
value)
{
$this->{$key} = $value;
}
}
}
$db = new database(array(
'database' => 'db_name',
'server' => 'database.internal',
));
In some rare situations, you actually want to say "the properties of this class are whatever I decide to add at run-time"; in that case, you can use the #[AllowDynamicProperties] attribute, like this:
#[\AllowDynamicProperties]
class objectWithWhateverPropertiesIWant {
public function __construct($params = array())
{
foreach ($params as
value)
{
$this->{$key} = $value;
}
}
}
Adding AllowDynamicProperties just above the class will make the warnings go away:
#[\AllowDynamicProperties]
class database {
Honest Question: Why did PHP remove dynamic properties in 8.x?
Creation of dynamic property is deprecated !
[Resolved] PHP: Creation of dynamic property is deprecated
Creation of dynamic property deprecated
Videos
I understand PHP has had many criticisms in the past but I'm not sure the existence of dynamic properties of instantiated objects was ever one of them. In fact, dynamic properties are pretty much the hallmark of most interpreted or dynamic programming languages. Python allows it all the time and so do many others like Ruby, Perl, etc.
I don't know what PHP developers achieved by removing dynamic properties feature from the language but one thing that resulted out of this is that many applications based on widely used veteran PHP frameworks (such as CodeIgniter and CakePHP) came to a halt all of a sudden due to an error like this after upgrading to PHP 8:
A PHP Error was encountered Severity: 8192 Message: Creation of dynamic property CI_URI::$config is deprecated Filename: core/URI.php Line Number: 102 Backtrace: File: C:\xampp\htdocs\inv_perpus\index.php Line: 288 Function: require_once
The influence of Corporate IT in various open source foundations is pretty well known and also well known is the extent to which corporate greed goes to achieve its interests and objectives across the world. The only way to assuage this uncomfortable thought (at least in this particular case) is to ask if there was any technical merit at all in removing dynamic properties feature from a dynamic programming language?
I for one couldn't find any such merit here.
Hi im creating dynamic property for my class objects for
example i assign database column to my Model dynamically
now all of this is deprecated how can i fix this problem?
Define the helper class like this
<?php
namespace ....
use Vendor\Modulename\Helper\Data;
class MyClass
{
protected $helper;
public function __construct(
Data $helper
) {
$this->helper = $helper;
}
}
Dynamic properties have been deprecated in php 8.2 and will gives fatal error from php 9.0 so declaring explicitly the properties is a more long-term solution than just use AllowDynamicProperties.
You can find a correct usage of this attribute here:
#[\AllowDynamicProperties]
class MyClass { }
or
use AllowDynamicProperties;
#[AllowDynamicProperties]
class MyClass { }
In my Magento 2 projects I explicitly declared the missing properties writing this script to avoid to do it manually. It:
- recursively open all the php files in a Magento module
- thanks to
ReflectionClassget information on constructor and properties - generate the property declarations missing
- add the declarations in the php file.