JAVA-PHP5 the Declaration of class instance variables

Source: Internet
Author: User
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.