I put some of the common sense of the PHP class out, so that the novice easier to master the PHP class, so that you can quickly understand the boss to write the program, oh, but less, I hope the boss to add or amend the error understanding.
Can classes in q:php have constructors?
A: You can, as long as the function name and the class name are the same, it will become a constructor. When you use the new operator to create an instance of a class, the constructor is called automatically, so you can put some initialization work in the constructor.
Does the class in q:php have a destructor?
A: No. The destructor is invoked automatically when the object is destroyed, using unset () or simply out of range. But there is no destructor in PHP. Such as:
Unset ($foo);
Destroys a class object $foo.
Q: How do I inherit a class?
A: Use the keyword extends to inherit a class, such as:
Class B extents A {
}
Q: How do you refer to yourself in the definition of a class?
A: Use $this to refer to the current class itself, such as:
$this->name = "Tom";
Q: How do I use the parent class in a subclass?
A: Use:: operator, and there are two ways, one is to use the parent class name directly, such as:
A::example ();
One is to use the parent keyword, such as:
Parent::example ();
It is recommended to use Parent:: To refer to the parental class.
Q: I don't want to create an instance of a class, can I use a function or a variable in a class directly?
A: Can, Adopt:: operator, such as:
B::example ();
You can perform function example () in class B without creating any object of Class B, just as a class function, which is good for the functional modules to induce the organization.
Does q:php have multiple inheritance? Added by Erquan
A: No. Subclasses cannot inherit from two parent classes at the same time, that is, a child cannot have two dads at the same time: Subclasses must depend on a base class that must already exist.
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