The Spring Festival is approaching. I will go home tomorrow. I am very excited. after a year of busy work, I can finally accompany my family. After a foreigner leaves Beijing, Beijing is like a 'Empty City'. Today, my colleagues talked about this and suddenly remembered a famous saying from Sima Qian: "The world is busy and busy, "Now, the direction of interest should be family, and the kite line is closed. no matter what you 've experienced in the past year, you have to clean up and go home for the new year. Wishing you a happy new year in advance ~ Before writing:
The Spring Festival is approaching. I will go home tomorrow. I am very excited. after a year of busy work, I can finally accompany my family. After a foreigner leaves Beijing, Beijing is like a 'Empty City'. Today, my colleagues talked about this and suddenly remembered a famous saying from Sima Qian: "The world is busy and busy, "Now, the direction of interest should be family, and the kite line is closed. no matter what you 've experienced in the past year, you have to clean up and go home for the new year. Wishing you a happy new year in advance ~
During the Spring Festival, I will also fulfill my commitment to myself and send at least one article each week.
I would also like to thank the readers of And1 for their valuable suggestions. if you have any suggestions, please let me know.
Regression text: Let's talk about classes in PHP today.
1. what is a class?
Class is to classify some things into one class, such as cats, dogs, and pigs. they are all animals, so we can classify them as one class. Classes are abstract and not specific. if a class is a class object, cats and dogs are an object in the animal class. Students can also serve as one type. then, Xiaohong is an object in the student class (the term in development is also called 'instance ')
In a rough sense, classes are a set of many methods (in fact, there are also attributes, attributes are only implemented in combination with methods). These methods are some logic or algorithms that you often use in programs, package them into the class to improve program efficiency and reduce code duplication.
Class 2 definition
The definition of each class starts with the keyword class, followed by the class name, followed by a pair of curly braces, which contains the class attributes and method definitions.
Var ;}}?>
When a method is called inside the class definition, there is an available pseudo variable $ this. $ This is a reference to a caller object. Actually, this class (pronoun)
Class 3 instances
An instance is a specific object. To create an instance of a class, you must use the new keyword.
If new is followed by a string containing the class name, an instance of the class is created.
// Create an instance
Class 4 inheritance
It can be understood that the child inherits the father's genes.
① A class can inherit the methods and attributes of another class with the extends keyword in the declaration.
② PHP does not support multiple inheritance. a class can only inherit one base class (in fact, a child can only have one parent ).
③ Inherited methods and attributes can be overwritten by re-declaring with the same name (it can be understood that the son has improved the father's original genes ). However, if final is used when the parent class defines a method, the method cannot be overwritten. You can use parent: To access overwriting methods or attributes.
④ When overwriting the method, the parameters must be consistent; otherwise, PHP will issue an E_STRICT error message. Except constructors, constructors can use different parameters when overwritten.
// Simple class inheritance
DisplayVar ();?>
Resolution of class 5 names
You can use ClassName: class to obtain a string that contains the fully qualified name of the class ClassName.
// Resolve the class name
// Output result: NS/ClassName
The above is the content of the PHP series (1). For more information, see The PHP Chinese website (www.php1.cn )!