PHP object-oriented Three-character Learning (full understanding of abstraction, encapsulation, inheritance, Polymorphism)

Source: Internet
Author: User
<span id="Label3"></p>PHP object-oriented Three characteristics of learning learning objectives: fully understand the abstract, encapsulation, inheritance, the three main characteristics of the multi-state surface orientation: encapsulation, inheritance, polymorphism First simple understanding of the Abstract:<br>When we define a class in front of us, we actually extract the properties and behaviors common to a kind of things, and form a physical model (template), the method of studying the problem is called abstract<br><strong><strong>first, the encapsulation of<br></strong></strong>Encapsulation is the extraction of data and the operation of the data is encapsulated together, the data is protected internally, the other parts of the program only authorized operations (methods) to manipulate the Data.<br>PHP provides three types of access control modifiers<br>Public indicates global, inside of this class, outside of class, subclass can access<br>Protected represents protected, only this class or subclass can access<br>Private means that only this class can be accessed internally.<br>The above three modifiers can be decorated with either a method or a property (variable), and if there is no access modifier, The default is public, the member property must specify an access modifier, and in PHP4 the Var $name, which means that the property is exposed and is not recommended<br>Cases:<br><span style="text-decoration: underline;"><span style="text-decoration: underline;">Copy Code</span></span>The code is as Follows:<br><?php<br>Class person{<br>Public $name;<br>Protected $age;<br>Private $salary;<br>function __construct ($name, $age, $salary) {<br>$this->name= $name;<br>$this->age= $age;<br>$this->salary= $salary;<br>}<br>Public Function Showinfo () {<br>This means that three modifiers can be used internally within this class<br>echo $this->name. "| |". $this->age. "| |". $this->salary;<br>}<br>}<br>$p 1=new person (' Zhang San ', 20,3000);<br>This belongs to the outside of the class, so if you use the following method to access age and salary will be an error<br>Echo $p 1->age; echo$p1->salary;<br>?><br>So what should I do now to access the elements and methods of protected and private externally? The common practice is to access these variable formats through the public function:<br>Public Function Setxxxx ($val) {<br>$this->xxxx= $val;<br>}<br>Public Function Getxxxx () {<br>Return $this->xxxx;<br>}<br>Here with set and get just for identification convenience, not necessary<br>Such as:<br>Public Function Getsalary () {<br>Return $this->salary; Extension: here can call some methods, such as determining the user name, and so on to access<br>}<br>externally, echo $p 1->getsalary () can be used.<br>If you want to access protected and private you can also use the following methods, but not recommended, as long as you understand<br>__set () and __get ()<br>__set () Assign a value to the protected or private property<br>__set ($name, $val);<br>__get () Gets the value of protected or private<br>__get ($name);<br>Such as:<br><span style="text-decoration: underline;"><span style="text-decoration: underline;">Copy Code</span></span>The code is as Follows:<br><?php<br>Class testa{<br>Protected $name;<br>Use __set () to manage all properties<br>Public function __set ($pro _name, $pro _val) {<br>The above $pro_name and $pro_val can be customized<br>The following $this->pro_name are fixed and cannot be changed<br>$this->pro_name= $pro _val;<br>}<br>Use __get () to get all the property values<br>Public Function __get ($pro _name) {<br>If (isset ($pro _name)) {<br>Return $this->pro_name;<br>} else {<br>Return null;<br>}<br>}<br>}<br>$n 1=new Testa ();<br>normally, the protected property cannot be accessed outside the class, but it can be manipulated using the above method<br>$n 1->name= ' small three ';<br>Echo $n 1->name;<br>?><br>The above code to understand the line, not recommended to use<br><strong><strong>second, the inheritance of</strong></strong> <br>Let's look at an example:<br><span style="text-decoration: underline;"><span style="text-decoration: underline;">Copy Code</span></span>The code is as Follows:<br><?php<br>Class pupil{<br>Public $name;<br>Protected $age;<br>Public Function GetInfo () {<br>echo $this->name. ' | | '. $this->age;<br>}<br>Public Function Testing () {<br>Echo ' This is pupil ';<br>}<br>}<br>Class graduate{<br>Public $name;<br>Protected $age;<br>Public Function GetInfo () {<br>echo $this->name. ' | | '. $this->age;<br>}<br>Public Function Testing () {<br>Echo ' This is graduate ';<br>}<br>}<br>?><br>As can be seen from the above example, when multiple classes have many common properties and methods, code reusability is not high, code redundancy, thinking about the processing methods in CSS<br>Workaround: inherit<br><span style="text-decoration: underline;"><span style="text-decoration: underline;">Copy Code</span></span>The code is as Follows:<br><?php<br>Class students{<br>Public $name;<br>Public $age;<br>Public function __construct ($name, $age) {<br>$this->name= $name;<br>$this->age= $age;<br>}<br>Public Function Showinfo () {<br>echo $this->name. ' | | '. $this->age;<br>}<br>}<br>Class Pupil extends students{<br>Function testing () {<br>Echo ' pupil '. $this->name. ' is testing ';<br>}<br>}<br>Class Graduate extends students{<br>Function testing () {<br>Echo ' Graduate '. $this->name. ' is testing ';<br>}<br>}<br>$stu 1=new pupil (' Zhang San ', 20);<br>$stu 1->showinfo ();<br>Echo ' <br/> ';<br>$stu 1->testing ();<br>?><br>As can be seen from the above, inheritance is a subclass (subclass) that inherits the properties and methods of public and protected in the parent class (baseclass) through the extends parent class, and cannot inherit private properties and methods<br>Syntax structure:<br>Class Parent class name {}<br>Class subclass name extends parent class name {}<br>Details:<br>1, a subclass can inherit only one parent class (this refers to direct inheritance); if you want to inherit the properties and methods of multiple classes, you can use multi-level inheritance<br>Cases:<br><span style="text-decoration: underline;"><span style="text-decoration: underline;">Copy Code</span></span>The code is as Follows:<br><?php<br>Class a{<br>Public $name = ' AAA ';<br>}<br>Class B extends a{<br>Public $age = 30;<br>}<br>Class C extends b{}<br>$p =new C ();<br>echo $p->name;//will output AAA here<br>?><br>2. When a subclass object is created, the constructor of its parent class is not automatically called by default<br>Cases:<br>Class a{<br>Public function __construct () {<br>Echo ' A ';<br>}<br>}<br>Class B extends a{<br>Public function __construct () {<br>Echo ' B ';<br>}<br>}<br>$b =new b ();//this will give precedence to the construction method in b, if there is no construction method in B to output the<br>3. In subclasses, If you need to access a method of a parent class (a constructor method, a modifier for a member method method, protected or private), you can use the parent class: either the method name or the parent:: method name to complete "both the parent and the previously mentioned self are lowercase, Uppercase Error "<br>Class a{<br>Public Function test () {<br>Echo ' A_test ';<br>}<br>}<br>Class B extends a{<br>Public function __construct () {<br>Both of these methods are OK<br>A::test ();<br>Parent::test ();<br>}<br>}<br>$b =new b ();<br>5, if the method of a subclass (derived Class) is exactly the same as the method of the parent class (public,protected), we are called method overrides or method overrides (override) to see the polymorphism below<br><strong><strong>Iii.</strong> polymorphism</strong> <br>Cases:<br><span style="text-decoration: underline;"><span style="text-decoration: underline;">Copy Code</span></span>The code is as Follows:<br><?php<br>Class animal{<br>Public $name;<br>Public $price;<br>Function Cry () {<br>Echo ' I don\ ' t know ';<br>}<br>}<br>Class Dog extends animal{<br>overwrite, Override<br>Function Cry () {<br>Echo ' Wang wang! ';<br>Animal::cry ();//this will not error, can correctly execute the parent class cry ();<br>}<br>}<br>$dog 1=new Dog ();<br>$dog 1->cry ();<br>?><strong><strong>Iv. Abstract class</strong></strong><p class="para"><p class="para">PHP 5 supports abstract classes and abstract methods. A class that is defined as abstract cannot be instantiated. Any class, if at least one of its methods is declared abstract, then the class must be declared Abstract. A method that is defined as abstract simply declares its invocation method (parameter) and cannot define its specific function Implementation.</p></p><p class="para"><p class="para">When inheriting an abstract class, the subclass must define all the abstract methods in the parent class, and the access control for these methods must be the same (or Looser) as the parent class. For example, If an abstract method is declared as protected, the methods implemented in the subclass should be declared as protected or public, and cannot be defined as Private. The method must be called in the same way, i.e. the type and the required number of parameters must be the Same. For example, A subclass defines an optional parameter, and the declaration of the parent abstract method does not, and the declaration does not conflict. This also applies to constructors from PHP 5.4. The constructor declaration before PHP 5.4 can be different.</p></p><p><p><strong>Example #1 Abstract Class Example</strong></p></p><pre><?<span style="color: #000000;"><span style="color: #000000;">PHP</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Abstract</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">abstractclass{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">forcing subclasses to define these methods</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Abstract</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">GetValue (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Abstract</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>Prefixvalue (<span style="color: #800080;"><span style="color: #800080;">$prefix</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Common methods (non-abstract Methods)</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">printOut () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Print</span></span> <span style="color: #800080;"><span style="color: #800080;">$this</span></span>->getvalue (). "\ n"<span style="color: #000000;"><span style="color: #000000;">; }}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>ConcreteClass1<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">abstractclass{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">getValue () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>"ConcreteClass1"<span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>Prefixvalue (<span style="color: #800080;"><span style="color: #800080;">$prefix</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>"{<span style="color: #800080;"><span style="color: #800080;">$prefix</span></span>}concreteclass1 "<span style="color: #000000;"><span style="color: #000000;">; }}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>ConcreteClass2<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">abstractclass{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span><span style="color: #000000;"><span style="color: #000000;">getValue () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>"ConcreteClass2"<span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>Prefixvalue (<span style="color: #800080;"><span style="color: #800080;">$prefix</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>"{<span style="color: #800080;"><span style="color: #800080;">$prefix</span></span>}concreteclass2 "<span style="color: #000000;"><span style="color: #000000;">; }}</span></span><span style="color: #800080;"><span style="color: #800080;">$class 1</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">ConcreteClass1;</span></span><span style="color: #800080;"><span style="color: #800080;">$class 1</span></span>-<span style="color: #000000;"><span style="color: #000000;">PrintOut ();</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Echo</span></span> <span style="color: #800080;"><span style="color: #800080;">$class 1</span></span>->prefixvalue (' foo_ '). " \ n "<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #800080;"><span style="color: #800080;">$class 2</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">ConcreteClass2;</span></span><span style="color: #800080;"><span style="color: #800080;">$class 2</span></span>-<span style="color: #000000;"><span style="color: #000000;">PrintOut ();</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Echo</span></span> <span style="color: #800080;"><span style="color: #800080;">$class 2</span></span>->prefixvalue (' foo_ '). " \ n "<span style="color: #000000;"><span style="color: #000000;">;</span></span>?></pre><p><p>The above routines will output:</p></p><pre><pre><span style="color: #000000;">Concreteclass1foo_concreteclass1concreteclass2foo_concreteclass2</span></pre></pre><p><p></p></p><p><p><strong>Example #2 Abstract Class Example</strong></p></p><pre><?<span style="color: #000000;"><span style="color: #000000;">PHP</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Abstract</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">abstractclass{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">our abstract method only needs to define the required parameters</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Abstract</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>Prefixname (<span style="color: #800080;"><span style="color: #800080;">$name</span></span><span style="color: #000000;"><span style="color: #000000;">);}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Concreteclass<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">abstractclass{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Our subclasses can define optional parameters that do not exist in the parent class signature</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>Prefixname (<span style="color: #800080;"><span style="color: #800080;">$name</span></span>,<span style="color: #800080;"><span style="color: #800080;">$separator</span></span>= "."<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(<span style="color: #800080;"><span style="color: #800080;">$name</span></span>= = "Pacman"<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #800080;"><span style="color: #800080;">$prefix</span></span>= "Mr"<span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">ElseIf</span></span>(<span style="color: #800080;"><span style="color: #800080;">$name</span></span>= = "pacwoman"<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #800080;"><span style="color: #800080;">$prefix</span></span>= "Mrs"<span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #800080;"><span style="color: #800080;">$prefix</span></span>= ""<span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>"{<span style="color: #800080;"><span style="color: #800080;">$prefix</span></span>}{<span style="color: #800080;"><span style="color: #800080;">$separator</span></span>} {<span style="color: #800080;"><span style="color: #800080;">$name</span></span>}"<span style="color: #000000;"><span style="color: #000000;">; }}</span></span><span style="color: #800080;"><span style="color: #800080;">$class</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">concreteclass;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Echo</span></span> <span style="color: #800080;"><span style="color: #800080;">$class</span></span>->prefixname ("Pacman"), "\ n"<span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Echo</span></span> <span style="color: #800080;"><span style="color: #800080;">$class</span></span>->prefixname ("pacwoman"), "\ n"<span style="color: #000000;"><span style="color: #000000;">;</span></span>?></pre><p><p></p></p> <p><p>The above routines will output:</p></p><pre><pre>Mr<span style="color: #000000;"> Pacmanmrs</span>. Pacwoman</pre></pre><p><p>In the old code, if no custom class or function is named "abstract", it should be able to work without Modification.</p></p><p><p></p></p><br>Summary:<br>1, when a parent class knows that all subclasses have a method, but the parent class is not sure how to write the method, you can let the subclass to overwrite its methods, method overrides (overrides), must require the subclass of the method name and the number of parameters exactly the same<br>2. If the subclass is going to call a method of the parent class (protected/public), you can use the parent class name:: method name or parent:: method name<br>3. When implementing a method override, the access modifier can be different, but the access rights of the subclass method must be greater than or equal to the access rights of the parent method (that is, access to the parent method cannot be reduced)<br>If the parent class public function cry () {} subclass protected function Cry () {} will error<br>however, the access rights of subclasses can be magnified, such as:<br>Parent class Private Function Cry () {} subclass protected function Cry () {} can execute correctly<br>Extended:<br>Method Overloading (overload)<br>Basic Concept: The function name is the same, but the number of parameters or parameters of different types, to call the same function, you can distinguish between different functions<br>Overloading is also supported in PHP5, but it is quite different from other languages, and it is not possible to define multiple functions with the same name in PHP<br>PHP5 provides a powerful "magic" function, using these magic functions, we can do function overloading,<br>Here we go to __call, when an object calls a method, and the method does not exist, the program will automatically call __call<br>"the Service is not recommended."<br>There are several magic constants in Php: __line__ __file__ __dir__ __function__ __class__, etc.<br>Cases:<br><span style="text-decoration: underline;"><span style="text-decoration: underline;">Copy Code</span></span>The code is as Follows:<br><?php<br>Class a{<br>function Test1 ($p) {<br>Echo ' test1<br/> ';<br>}<br>function Test2 ($p) {<br>Echo ' test2<br/> ';<br>}<br>function __call ($method, $p) {<br>Here $p is an array, the above two variable names can be customized<br>if ($method = = ' Test ') {<br>If (count ($p) ==1) {<br>$this->test1 ($p);<br>} else if (count ($p) ==2) {<br>$this->test2 ($p);<br>}<br>}<br>}<br>}<br>$a =new a ();<br>$a->test (5);<br>$a->test (3,5);<br>?><p><p>PHP object-oriented Three-character Learning (full understanding of abstraction, encapsulation, inheritance, Polymorphism)</p></p></span>

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.