Php combination, php arrangement and combination algorithm _ PHP Tutorial

Source: Internet
Author: User
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 {...

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.