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 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 SyntaxHighlighter. all ();
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 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); 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 to reference itself in the class definition? A: use $ this to reference the current class itself, for example, $ this-> name = "Tom"; Q: How to use the parent class in the subclass? A: Use the: operator. There are two methods: use the parent class name directly, for example, A: example (); use the parent keyword, for example: 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. you can use the: operator, such as B: example (); to execute the function example () in class B without creating any Class B objects, it is called only as a class function, which is very good for functional modules to organize. 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.