Php combination and php arrangement and combination algorithm. Php combination and php arrangement and combination algorithm in order to improve code reusability and reduce code coupling (two methods of combination implementation) Mode 1: 1? Php2 combination mode 3 classPerson {php combination, php arrangement combination algorithm
To improve code reusability and reduce code coupling (two methods of combination implementation)
Mode 1:
1
"; 6} 7} 8 9 class Phone {10 public function call () {11 echo" phone call.
"; 12} 13} 14 15 // students also need the call () method to improve code reusability (combination) 16 class Student extends Person {17 private $ people; 18 public function learning () {19 echo "learn.
"; 20} 21 public function func ($ class, $ method) {// compatible with multiple methods of multiple classes 22 $ this-> people = new $ class; 23 $ this-> people-> $ method (); 24} 25} 26 27 $ student = new Student (); 28 $ student-> eat (); 29 $ student-> func ('phone', 'call'); 30 $ student-> learning ();
Mode 2:
1 "; 6} 7} 8 9 trait Drive {10 public function call () {11 echo" phone call.
"; 12} 13} 14 15 class Student extends Person {16 use Drive; 17 public function learning () {18 echo" learn.
"; 19} 20} 21 22 $ student = new Student (); 23 $ student-> eat (); 24 25 // when the method or attribute has the same name, the methods in the current class overwrite the trait method, and the trait method overwrites the methods in the base class 26 $ student-> call (); 27 $ student-> learning ();View Code
In order to improve code reusability and reduce code coupling (two methods of combination implementation), Example 1: 1? Php 2 // combination mode 1 3 class Person {...