PHP hook and simple Distribution Method Instance analysis, hook distribution instance analysis

Source: Internet
Author: User

PHP hook and simple Distribution Method Instance analysis, hook distribution instance analysis

This article describes PHP hooks and simple distribution methods. We will share this with you for your reference. The details are as follows:

// Simple hook implementation example class tool {public static function main ($ class, $ fun, $ data = '') {// pre-public operation $ con = new $ class; $ con-> $ fun ($ data); // post public operations} class a {function B ($ data) {echo 'method B ';}} class c {function d ($ data) {echo 'method d';} // call tool: main ('A', 'B ', '123 ');

The hooks are intended to be used to encapsulate the SMS channel,

Messages such as SMS (Multi-Channel) email and push can be sent automatically...

Later I found that the business needs were not as complicated as I thought, and the development was put on hold ....

T_T uses a simple distribution method.

Class Ar_Sms {const LANCHUANG = 1; // Channel 1 const ALIDAYU = 2; // Channel 2 private $ type; private $ chuanglan_config = array (// Channel 1 configuration item 'api _ send_url '=> 'xxx', 'api _ balance_query_url' => 'xxxxx ', 'api _ account' => 'xxxx ', 'api _ password' => 'xxxxx ',); private $ alidayu_config = array (// Channel 2 configuration item 'api _ key' => 'xxx', 'api _ id' => 'xxxxx ', 'api _ send_url '=> 'xxxxx',); public function _ construct ($ type = 1) {switch ($ type) {cas E self: LANCHUANG: $ this-> type = $ type; break; case self: ALIDAYU: $ this-> type = $ type; break; default: $ this-> type = false ;}/// public function sendSms ($ mobile, $ msg) {switch ($ this-> type) {case self:: LANCHUANG: return $ this-> _ sendCL ($ mobile, $ msg); case self: ALIDAYU: return $ this-> _ sendAL ($ mobile, $ msg ); default: return false ;}// method of sending Channel 1 private function _ sendCL ($ mobile, $ msg, $ need Status = 'false', $ extno = '') {$ postArr = array ('account' => $ this-> chuanglan_config ['api _ account'], 'pswd '=> $ this-> chuanglan_config ['api _ password'], 'msg' => $ msg, 'mobile' => $ mobile, 'needstatus' => $ needstatus, 'extno' => $ extno); $ result = $ this-> _ curlPost ($ this-> chuanglan_config ['api _ send_url '], $ postArr); $ result = $ this-> _ execResult ($ result); return $ result [1] = 0? True: $ result [1];} // method of sending Channel 2 private function _ sendAL ($ mobile, $ msg) {$ postArr = array ('id' => $ this-> alidayu_config ['api _ id'], 'key' => $ this-> alidayu_config ['api _ key'], 'msg '=> $ msg, 'mobile' => $ mobile ,); $ result = $ this-> _ curlPost ($ this-> alidayu_config ['api _ send_url '], $ postArr ); $ result = $ this-> _ execResult ($ result); return $ result [1] = 0? True: $ result [1];} // ------------- some common methods/*** process returned values \ r \ n split **/private function _ execResult ($ result) {$ result = preg_split ("/[, \ r \ n]/", $ result); return $ result ;} /*** processing returned value json **/private function _ jsonResult ($ result) {$ result = json_decode ($ result, true); return $ result ;} /*** send an HTTP request through CURL * @ param string $ url // request URL * @ param array $ postFields // request parameter * @ return mixed */private function _ curlPost ($ url, $ postFields) {$ postFields = http_build_query ($ postFields); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_HEADER, 0 ); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ postFields ); $ result = curl_exec ($ ch); curl_close ($ ch); return $ result ;}}$ ob = new Ar_Sms (Ar_Sms: ALIDAYU ); // Channel 1 sends $ res = $ ob-> sendSms ('xxxxx', 'xxxxxx'); var_dump ($ res); $ ob = new Ar_Sms (Ar_Sms :: LANCHUANG); // Channel 2 sends $ res = $ ob-> sendSms ('xxxxx', 'xxxxxx'); var_dump ($ res );

Multi-channel free switching is achieved through a simple distribution,

As it is only simple to send, there is no further abstraction ~ Orz

The implementation method of the hook is a bit big, and the specific implementation is also well-developed .. If you have time, I will study it. Here is a simple demo. Imagine: "multi-channel, multi-mode, multi-channel support, and convenient extension ."

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.