Declaration of class instance variables for java-PHP5

Source: Internet
Author: User
{Code ...} as shown above, if an instance variable is declared directly in the php5 class, the class declaration error will be reported and changed to the following so that the {code ...} it is necessary to declare a null value first, and then assign a value using the method. it is often written for a long time and no bug is found, so that we can understand that it is a syntax requirement. I don't understand...
private $db = new Db();

As shown above, if an instance variable is declared directly in the php5 class, the class declaration error will be reported.

Change it to the following.

private $db = null;    public function __construct(){    $this->db = new Db();    }

It is necessary to declare a null value first, and then assign a value using the method. it is often written for a long time and no bug is found, so that we can understand that it is a syntax requirement.

I don't understand what it is based on. I understand that it may be because php5 is still not well-developed in terms of object learning, so there are a lot of questions for everyone to talk about.

Q: Is PHP7 still like this?

Laruence, please @ Laruence
.

Reply content:
private $db = new Db();

As shown above, if an instance variable is declared directly in the php5 class, the class declaration error will be reported.

Change it to the following.

private $db = null;    public function __construct(){    $this->db = new Db();    }

It is necessary to declare a null value first, and then assign a value using the method. it is often written for a long time and no bug is found, so that we can understand that it is a syntax requirement.

I don't understand what it is based on. I understand that it may be because php5 is still not well-developed in terms of object learning, so there are a lot of questions for everyone to talk about.

Q: Is PHP7 still like this?

Laruence, please @ Laruence
.

Because if you allow
private $db = new Db();
With this syntax, the Db class is initialized after the file is loaded.
Note that Class A is loaded into the memory and the Db class is instantiated.
If the Db class uses the same syntax, it may be slow to include a file, because at the same time, a series of class instantiation behaviors are being performed.

To disable this feature, you can only instantiate other classes in the class constructor to control the class instantiation process. When a class is loaded, the attached class instantiation will never be generated.

Whether it is system optimization or running time control, it is good and sustainable to be predictable and controllable.

PHP currently does not support direct assignment of objects to object attributes. object assignment can only be implemented through constructors.
Of course, you can't say that php is going to learn object-oriented from java, and no one specifies that java is the benchmark for object-oriented development. If php is exactly the same as java, why is it still called php.

A: PHP 7 is still like this.

The direct value assignment of class can only be a scalar (value, string, array, Boolean)

Thank you for your invitation. The answer upstairs is in place.

They are all great gods who haven't done it before directly instantiating classes outside the function, but your second method is that two DBs are not the same variable.

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.