Home >Backend Development >PHP Tutorial >java - PHP5 class instance variable declaration problem
<code>private $db = new Db();</code>
As above, in a PHP5 class, if you directly declare an instance variable, a class declaration error will be reported
Change it to the following
<code>private $db = null; public function __construct(){ $this->db = new Db(); }</code>
You have to declare a null value first, and then use a method to assign the value. It often takes a long time to write and check to find out that there are no bugs, and then you realize that it is a grammatical requirement.
I don’t understand what the considerations are based on. I understand that it may be that the object learning in php5 is not perfect yet, so there are many questions. Let’s talk about it.
Additional question: Is this still the case with PHP7?
Brother Bird, please fuck @Laruence
.
<code>private $db = new Db();</code>
As above, in a PHP5 class, if you directly declare an instance variable, a class declaration error will be reported
Change it to the following
<code>private $db = null; public function __construct(){ $this->db = new Db(); }</code>
You have to declare a null value first, and then use a method to assign the value. It often takes a long time to write and check to find out that there are no bugs, and then you realize that it is a grammatical requirement.
I don’t understand what the considerations are based on. I understand that it may be that the object learning in php5 is not perfect yet, so there are many questions. Let’s talk about it.
Additional question: Is this still the case with PHP7?
Brother Bird, please fuck @Laruence
.
Because if syntax like
<code>private $db = new Db();</code>
is allowed, then the Db class will be initialized after the file is loaded.
Please note that class A is loaded into memory and class Db is instantiated.
If the Db class also uses the same syntax, then it may be very slow if you include a file, because a series of class instantiation actions are being performed at the same time as the include.
By disabling this feature and only allowing other classes to be instantiated in the class constructor, you can better control the class instantiation process. When a class is loaded, an accompanying class instantiation is never produced.
Whether it is for system optimization or control of program running time, what is predictable and controllable is good and sustainable.
PHP currently does not provide support for directly assigning object attributes to objects. The assignment of objects can only be achieved through constructors for the time being.
Of course, you can’t say that PHP should learn object-oriented from Java, and no one stipulates that Java is the benchmark for object-oriented. If PHP is really the same as Java in everything, then why is it still called PHP?
Answer: This is still the case with PHP7.
Direct assignment of class can only be scalar (numeric value, string, array, Boolean)
Thanks for the invitation. The answer above is spot on
They are all experts. I have never instantiated a class directly outside the function, but your second way of writing the two dbs are not the same variable