private $db = new Db();
As above, in the class of PHP5, if an instance variable is declared directly, the class declaration error is reported
Change it to the bottom so you can
private $db = null; public function __construct(){ $this->db = new Db(); }
Must first declare a null value, and then use the method to assign value, often write to look for half a day to find no bug, only to understand the original grammar requirements.
Do not understand is based on what considerations, I understand, may be php5 learning surface as the object of the study is not perfect, so there are a lot of problems, everyone talk.
Q: PHP7 still like this?
Brother Bird, please @Laruence
.
Reply content:
private $db = new Db();
As above, in the class of PHP5, if an instance variable is declared directly, the class declaration error is reported
Change it to the bottom so you can
private $db = null; public function __construct(){ $this->db = new Db(); }
Must first declare a null value, and then use the method to assign value, often write to look for half a day to find no bug, only to understand the original grammar requirements.
Do not understand is based on what considerations, I understand, may be php5 learning surface as the object of the study is not perfect, so there are a lot of problems, everyone talk.
Q: PHP7 still like this?
Brother Bird, please @Laruence
.
Because if you allow
private $db = new Db();
Such a syntax, then, after the file is loaded, the DB class is initialized.
Note that the class A is loaded into memory and the DB class is instantiated.
If the DB class also uses the same syntax, then you might include a file that is slow because the include is a series of class instantiation behaviors.
Disabling this feature allows the class instantiation process to be well controlled by only allowing other classes to be instantiated in the class construction method. When a class is loaded, the accompanying class instantiation is never generated.
Whether for the optimization of the system, or the control of the program running time, predictable, controllable, is good, sustainable.
PHP temporarily does not provide support for object properties directly assigned to objects, the assignment of objects can only be implemented by constructors temporarily.
Of course, you can not say that PHP to Java to learn object-oriented, and there is no rule that Java is the object-oriented benchmark, if PHP is really all the same as Java, then why it is called PHP it.
Answer: PHP7 is still the case.
The direct assignment of class can only be scalar (numeric, string, array, Boolean)
Thank you for inviting me. Upstairs, the answer is in place.
It's all great gods. Directly instantiating a class outside of a function has not been done, but your second way of writing two DB is not the same variable.