PHP classes and objects to use the scene

Source: Internet
Author: User

Reproduced:

<?php/*** = = = Note Part = = The specific syntax of the interface: 0: Human As, class Human is a human sketch and interface is a part can be combined with a variety of parts of a new special.  1: As above, the interface itself is abstract, the method of internal declaration is also abstract by default.  No addition to abstract 2: A class can implement multiple interfaces at once.  The syntax is implemented using implements (which implements my several functions) class ClassName implements Interface1,interface2,interface3 {} and then the function of the interface is implemented. 3: Interface can also inherit, with extends 4: interface is a description of a bunch of methods, cannot add attribute 5: interface is for assembly into class use, closed up meaningless, so the method can only be public ***/interface Animal {protected   function eat ();       } interface Monkey extends animal {public function run ();   Public function Cry ();   } Interface Wisdom {public function think ();   } interface Bird extends animal{public function fly ();          }/*//error below, Monkey inherited Aniaml interface, so you must implement class Human implements Monkey,wisdom {public function run () {eat ()) {      Echo ' Walk ';      } Public Function Cry () {echo ' cry ';      } public Function Think () {echo ' think ';       }} */class Human implements Monkey,wisdom {public function eat () {echo ' eats ';  }     Public Function run () {echo ' walk ';       } Public Function Cry () {echo ' cry ';       } public Function Think () {echo ' think ';   }}?> <?php//04.php/*** = = = Note Part = = A point of view of object-oriented: The more you do, the easier it is to make mistakes abstract class {defines class template}--concrete subclass implementation {China,japan,english}  Interface: ***///Abstract Database class/* What database do you use to start a website?  MySQL, Oracle,sqlserver,postgresql?  This: first develop the website, run again.  First to get a MySQL development, the official launch of the database is not too late to attract problems: Change the database, will the previous code to be rewritten?  A: No, it is not necessary to use abstract class developers, when developing, the DB abstract class to develop.       */abstract class DB {public abstract function connect ($h, $u, $p);       public abstract function query ($sql);   Public abstract function close ();           }/*//The following code is wrong//because when the subclass is implemented, the connect parameter of the Connect and abstract class is inconsistent class MySQL extends DB {public function connect ($h, $h) {      return true;   The Public Function query ($sql, $conn) {} public Function Close () {}} */* below this MySQL class, strictly implements the DB abstract class Imagine: No matter what database I actually use on the go, I just need to write one more of the following classes class Oracle extends DB {} class MSSQL extends DB {} class Postsql extends db {} Do not change the business logic layer?  Why don't you change it?  Because all implementations of the DB abstract class.  When I develop, I can refer to the DB abstract class where I call the method unclear.  Anyway, subclasses are strictly implemented abstract classes.       */class MySQL extends DB {public function connect ($h, $h, $u) {return true; The Public Function query ($sql) {} public Function Close () {}}/* Interface is more abstract such as a social networking site, about the user  Processing is the core application. Landing quit letter Greeting change mood eat curse trouble show love so many ways, are users of the method, can naturally write a user class, all packaged up but, analysis user one-time not so method user information class: {Landing, letter, read letter, greeting, more  Change mood, quit} User Entertainment class: {login, Curse, trouble, show Love, Tease, quit} before the development of the site, analysis out so many methods, but can not be installed in a class, divided into 2 classes, or even more.  The role of application logic development, so many classes, so many methods, are dizzy.       */interface UserBase {public Function login ($u, $p);   Public function logout ();       } interface Usermsg {public Function wirtemsg ($to, $title, $content);   Public Function readmsg ($from, $title);       } interface Userfun {public Function spit ($to);   Public Function Showlove ($to);  }/* As the caller, I do not need to know your user information class, user entertainment class, I can know how to call these two classes because: these two classes to implement the above interface.  Through this interface, it is possible to standardize development. */  /* Below this class, and the parameters of the interface declaration is not the same, the error, so that the interface force unified the functionality of the class no matter how many classes you have, there are several methods in a class that I only know that methods are methods of implementing interfaces.   */class User implements UserBase {public function login ($u) {}}?>

  

PHP classes and objects to use the scene

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.