Php code object-oriented basics this article is not suitable for beginners. if you have a certain understanding of php, you can take a look at some of the features of php object-oriented.
1. what is object-oriented?
Although I know a little about it, I still feel that I can't take it out. I can only say that everything is regarded as an object. only in development can I understand what object-oriented is, however, because php is mostly used for web development, it can run well even if it is not used for object-oriented development. in the previous development of c ++, we designed a functional interface for you to see this interface, the first thing is to cut objects one by one like an artist, then determine the relationship between objects, and finally develop them. this idea is everywhere.
What is a class? What is an object?
A class is an abstraction of a set. what set? Is a set of objects with similar features and operations.
An object is a specific instance of a class.
I may give it back to the interviewer at graduation, but now, although I still want to understand the same conventions as statements in books, at least I don't need to rely on brain cells to express these two definitions.
II. class structure in php
Php classes are also those access controllers and have attributes and methods.
class Person { private $name = "PersonName"; public static $gender = "PersonGender"; public function test(){ echo $this->name, '
'; }};
III. constructor
The name of the constructor is _ construct. here I want to emphasize the following points:
1. php will not automatically call your parent class constructor when instantiating a subclass like other languages (c ++ or java). php needs to manually call the constructor of the parent class, inheritance is involved here. let's take a look at Inheritance first.
Class Person {public funciton _ construct () {echo 'person construct
';}}; Class Teacher extends Person {public function _ construct () {// parent :__ construct (); echo 'teacher construct
';}}; $ T1 = new Teacher; // Generate a Teacher object
Running result:
Teacher construct
To initialize data of the parent class when a subclass is generated, you must manually call the constructor of the parent class and open the comment line.
2. two constructors with different parameters cannot be written in a class.
Some of the rules in php are involved here. the following statements are correct in other languages:
class Person{public funciton __construct(){echo 'Person construct
';}public function __construct($param){echo 'Person with param construct
';}};
Php is not allowed. the root cause is that php is a weak language type and is not very sensitive to type restrictions. in this way, the _ call and func_get_args function mechanisms are provided, therefore, the following methods can be used:
Class Person {public function _ construct () {$ param = func_get_arg (); // Get the parameter data $ param_num = func_num_args (); // Obtain the number of parameters if ($ param_num = 0) {} else if ($ param_num = 1) {if (is_array ($ param [0]) {//...}} else {//...}}};
III. destructor
The Destructor is a function automatically called when the instance object ends. you can write a statement to release the memory to end the death of the instance. this is the same as the structure, when there is an inheritance relationship, you must manually call the structure of the parent class. one class has only one structure.
4. access control characters
Public: A public Access character. this attribute or method can be accessed in the class, in the subclass, and out of the class.
Protected: a protected access operator that can only access this attribute or method within the class and its subclass, but not outside the class.
Private: a private access character, which can only be accessed within this class. it belongs to this class. it cannot be inherited, reload, or accessed by anyone.
5. magic methods _ get and _ set
The functions of these two methods: an accesser for protected and private attribute access can perform security and rational verification on the data received from external class.
The _ set method receives two parameters. The first parameter is the attribute name, and the second parameter is the new value to be assigned.
The _ get method receives a parameter named attribute.
1. public attributes can be used to modify attributes outside the class. Therefore, for public attributes, the _ get and _ set processes are not followed.
class D{ public $name = 'D name'; protected $gender = 'male'; private $age = 18; public function __set($name, $value){ echo '__set
';//if(in_array($name, ['name', 'gender', 'age']))$this->$name = $value; } public function __get($name){ echo '__get
';//if(!in_array($name, ['name', 'gender', 'age'])) return NULL; return $this->$name; }};
Running result:
New D name // name is the public attribute and does not go through get and set _ getnew D gender _ set _ getnew D age
2. you can also add the data verification function to enable the annotation to perform verification.
3. the two methods must be public access. Otherwise, an error will be prompted. we can think about the functions of these two functions, so it is hard to imagine why public access control is required.
6. Inheritance
At last, the characteristics that can make your chrysanthemum bloom are not inherited, and all classes are scum. because of inheritance, so... the question is that a great wave of things is coming... let's take a closer look at this inheritance feature.
Let's not talk about inheritance. there is a small example of inheritance at the beginning of the article.
What are the problems with inheritance? Think about it. if B inherits A... it is really hard to imagine...
1. constructor, which has nothing to do with Inheritance. it is equivalent to the constructor in two classes. but there is also a parent-child relationship, so it cannot be done too absolutely, as mentioned above, if necessary, you can manually call the constructor of the parent class. you can refer to the above example.
2. single-direction inheritance: single-direction Inheritance. child classes can inherit from the parent class, but the parent class cannot inherit from the child class. for example:
Class A {public $ attr1; public function oper1 () {}}; class B extends A {public $ attr2; public function oper2 (){}}; // subclass can inherit the parent class $ B = new B; $ B-> oper1 (); $ B-> attr1 = 'attr1 value'; $ B-> oper2 (); $ B-> attr2 = 'attr2 value'; // the parent class cannot inherit the subclass $ a = new A; $ a-> oper2 (); // error $ a-> attr1 (); // error
3. Reload: it is especially awkward to mention php reload, because its reload is put in other languages called overwrite, and I am still used to rewrite this feature.
<1> public Overload:
class E{ public $attr1 = 'E attr1 value'; public function oper1(){ echo 'E oper1
'; echo 'attr1 value = ', $this->attr1, '
'; }};class F extends E{ public $attr1 = 'F attr1 value'; public function oper1(){ //parent::oper1(); echo 'F oper1
'; echo 'attr1 value = ', $this->attr1, '
'; }};$f = new F;$f->oper1();
Running result:
F oper1
Attr1 value = F attr1 value
F inherits E and overrides E's attr1 and oper1. Therefore, when oper1 is called, $ this-> attr1 displays F attr1 value. if you open the comment parent :: oper1 calls the Oper1 method of the parent class. the running result is as follows:
E oper1
Attr1 value = F attr1 value // The attr1 attribute has been overwritten by the attr1 attribute of the quilt class.
F oper1
Attr1 value = F attr1 value
It can be seen that after the subclass overrides the attributes and methods of the parent class, the corresponding attributes and methods of the parent class will be overwritten.
<2> private overload
class E{ private $attr1 = 'E attr1 value'; public function oper1(){ echo 'E oper1
'; echo 'attr1 value = ', $this->attr1, '
'; }};class F extends E{ public $attr1 = 'F attr1 value'; public function oper1(){ parent::oper1(); echo 'F oper1
'; echo 'attr1 value = ', $this->attr1, '
'; }};$f = new F;$f->oper1();The above code only changes one place, that is, the access attribute of the parent class $ attr1 is changed to private. how can we execute the overload mechanism? Check the running result first:
E oper1
Attr1 value = E attr1 value // private attribute of the parent class
F oper1
Attr1 value = F attr1 value
As we have said before, private attributes and method subclasses cannot be inherited. in this case, the following principle is followed:
If the private attribute is called in that class, the attribute values in the class are displayed.
In the example, the parent: oper1 method calls the oper1 method of Class E, and $ this-> attr1 is called in the oper1 method of Class E. attr1 is private and does not inherit from the quilt class, therefore, the attr1 attribute value in Class E is called.
<3> the protected overload is consistent with the public overload.
<4> inheritance of class attributes
class G{ public static $attr1 = 'G attr1 value'; public function oper1(){ echo 'G oper1
'; echo 'attr1 value = ', self::$attr1, '
'; }};class H extends G{ public static $attr1 = 'H attr1 value'; public function oper1(){ parent::oper1(); echo 'H oper1
'; echo 'attr1 value = ', self::$attr1, '
'; }};$h = new H;$h->oper1();
Running result:
G oper1
Attr1 value = G attr1 value
H oper1
Attr1 value = H attr1 value
In fact, no matter whether the attr1 attribute of the G class is public or private, the results are the same.
In my personal understanding, class attributes can be inherited, but not heavy loads. There is also a rule for the attributes of the parent class called by subclass:
Self or parent only represents this class. Therefore, according to this principle, we can conclude that the attribute value must be the value of this class attribute, and it has nothing to do with the subclass. (In special cases, php's static delay loading mechanism ).
VII. static delayed loading
Now that we have mentioned static latency loading, let's take a look at the example of H and G. What should I do if I want to call the parent class in the subclass? Static delayed loading solves this problem. see two examples:
Example 1:
Examples of H and G classes
class G{ public static $attr1 = 'G attr1 value'; public function oper1(){ echo 'G oper1
'; echo 'attr1 value = ', static::$attr1, '
'; }};class H extends G{ public static $attr1 = 'H attr1 value'; public function oper1(){ parent::oper1(); echo 'H oper1
'; echo 'attr1 value = ', self::$attr1, '
'; }};$h = new H;$h->oper1();Running result:
G oper1
Attr1 value = H attr1 value
H oper1
Attr1 value = H attr1 value
The above code only changes self: $ attr1 in the G class to static: $ attr1, and the running result is different.
Example 2:
class I { public static function who(){ echo __CLASS__, '
'; } public static function test(){ static::who(); }};class J extends I{ public static function who(){ echo __CLASS__,'
'; }};
Running result:
J
Through these two examples, we can better understand static latency binding.
There are a lot of writing, mainly because the inheritance will not be able to stop .... there are still some points of object-oriented work. you can add them later... thank you. if there are any errors, please make them correct at any time. thank you !!