[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.
...