A small rookie, has been not understand namespace, abstract, interface, trait these relations, take a few days to study, do a summary, the shortcomings of hope that everyone to correct the exchange.
namespace namespaces
Introduction: As the name implies, is to give space name. In fact, the simplest can be understood as a space for each folder, and then we give this space a definition (namespace), let it become a formal space. Of course not to say that a folder requires a space (preferably), in the same folder, files can also define a different space.
Role: If there are two classes (two folders), there is a student called test (file), when two people need to appear at the same time, we should call them do not confuse? Obviously, we can take the forward prefix, for example, a tall test, a handsome test, or a class of test, which is the equivalent of a declaration. The namespace, in other words, declares a space, which can be called Class1 or height1. Therefore, the most important role of the namespace is to solve the PHP internal class/function/constant or third-party class/function/constant name conflict, but also to improve the source of the reading. As below, in a file, using namespace we do not invoke the wrong method or class.
For the use of abstract, interface, trait, please see the following examples (I am not familiar, will continue to update):
<?php/* ============ Interface class =============*/namespace Payinterface;interface payinterface{//must be publicly public function que Ry ($transaction _number); Query Payment information public function check ($card _info) according to serial number; Verify payment information public function pay ($card _info); Pay Public Function refund ($transaction _number); Refunds based on serial number}/* =================namespace itemplate end==================== *//* ============ abstract class =============*/ namespace Payway;abstract class payway{abstract protected function WeChat ();//Pay abstract public function Alipay ( ); The method declaration of the Ali payment//Inheritance class must be the same as the parent class (or more lenient), and the parent class method cannot be private//common method (non-abstract method) and cannot define its specific functionality to implement Public function test () { Echo ' <br>===================<br> '; Echo ' This is an abstract class common method that subclasses can use '; Echo ' <br>===================<br> '; }}/* ================namespace abstractclass end===================== *//** payment method specific implementation */namespace Wechat;use Payinterface\payinterface;class WeChat implements payinterface{Public function query ($transAction_number) {echo ' <br>===================<br> '; Echo ' This is WeChat query '. $transaction _number; Echo ' <br>===================<br> '; } Public Function Check ($card _info) {echo ' <br>===================<br> '; Echo ' This is WeChat check '. $card _info; Echo ' <br>===================<br> '; Public function Pay ($card _info) {echo ' <br>===================<br> '; Echo ' This was WeChat pay '. $card _info; Echo ' <br>===================<br> '; } Public Function refund ($transaction _number) {echo ' <br>===================<br> '; Echo ' This is WeChat refund '. $transaction _number; Echo ' <br>===================<br> '; }}/* =================namespace WeChat end==================== *//** Ali payment method specific implementation */namespace Alipay;use payinterface\ Payinterface;class AliPay implements payinterface{ Public Function query ($transaction _number) {echo ' <br>===================<br> '; Echo ' This is AliPay query '. $transaction _number; Echo ' <br>===================<br> '; } Public Function Check ($card _info) {echo ' <br>===================<br> '; Echo ' This is AliPay check '. $card _info; Echo ' <br>===================<br> '; Public function Pay ($card _info) {echo ' <br>===================<br> '; Echo ' This was AliPay pay '. $card _info; Echo ' <br>===================<br> '; } Public Function refund ($transaction _number) {echo ' <br>===================<br> '; Echo ' This is AliPay refund '. $transaction _number; Echo ' <br>===================<br> '; }}/* =================namespace Alipay end==================== *//* ============trait=============*/namespace Sayworld;trait Sayworld { static function SayHello ($STR) {echo ' <br>===================<br> '; Echo ' world! This is '. $STR; Echo ' <br>===================<br> '; }}/* ================namespace sayworld end===================== */namespace pay;use payway\PayWay;use wechat\WeChat; Use Alipay\alipay;use Sayworld\sayworld; Class pay extends payway{use Sayworld;//No instantiation of public function WeChat () {Sayworld::sayhello (' WeChat '); return new WeChat; } public Function Alipay () {Sayworld::sayhello (' Alipay '); return new Alipay; }}/* ================namespace pay end===================== *//* ============ instantiate =============*/use pay\Pay; $pay = new Pay (); $pay->wechat ()->query (' 1234 '); $pay->alipay ()->query (' 1234 '); $pay->test ();
Output Result:
PHP namespace, abstract, interface, trait usage introduction