[FAQ] Some common knowledge in PHP: class_php tutorial

Source: Internet
Author: User
[FAQ] Some common knowledge in PHP: class. I sorted out the common knowledge of some PHP classes to make it easier for new users to master the PHP classes, so that they can quickly understand the programs written by those elders, I hope that the old guys will help me sort out the common knowledge of some PHP classes to make it easier for new users to master PHP classes so that they can quickly understand the programs written by the old guys, I hope you can add or correct the errors.

Q: Can classes in PHP have constructors?
A: Yes. as long as the function name and class name are the same, this function will become A constructor. When the new operator is used to create an instance of a class, the constructor automatically calls the constructor. Therefore, some initialization work can be completed in the constructor.

Q: Do classes in PHP have destructor?
A: No. When an object is destroyed and unset () or simply out of range is used, the destructor is automatically called. However, PHP does not have destructor. For example:
Unset ($ foo );
Destroy a class object $ foo.


Q: How do I inherit a class?
A: use the keyword extends to inherit A class, such:
Class B extents {
}


Q: How to reference itself in class definition?
A: use $ this to reference the current class itself, for example:
$ This-> name = "Tom ";


Q: How do I use a parent class in a subclass?
A: Use the: operator. There are two methods: use the parent class name directly, for example:
A: example ();
One is to use the parent keyword, such:
Parent: example ();
We recommend that you use parent: to reference the parent class.


Q: I don't want to create a class instance. can I directly use functions or variables in the class?
A: Yes. use the: operator, for example:
B: example ();
You can execute the function example () in class B without creating any Class B objects. it is only called as a class function, which is good for the function module induction and organization.


Q: Does PHP have multiple inheritance? // Added by erquan
A: No. Child classes cannot inherit from two parent classes at the same time, that is, a child cannot have two fathers at the same time :). The subclass must depend on an existing base class.

...

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.