Detailed Introduction to PHP hooks and simple distribution methods

Source: Internet
Author: User
This article mainly introduces PHP hooks and simple distribution methods, combined with examples of the definition and use of PHP hooks, and the implementation of the multi-channel free switch, the need for friends can refer to the next

This article describes the PHP hooks and simple distribution methods. Share to everyone for your reference, as follows:


The simple hook implementation example class tool{  public static function main ($class, $fun, $data = ") {    //Pre-common operation    $con = new $class; 
  
    $con $fun ($data);    Post-public Operation  }}class a{  function B ($data) {    echo ' I am method B ';  }} Class c{  function D ($data) {    echo ' I am method d ';  }} The hook calls Tool::main (' a ', ' B ', ' 222 ');
  

This was intended to be achieved with hooks when encapsulating the SMS channel.

can automatically send SMS (multi-channel) Email,push and other messages ...

Later found that business requirements are not as complex as imagined, development is shelved ....

T_T then adopted a simple distribution method to achieve


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 ' = ' xxxx ', ' api_balance_query_url ' = ' xxxxx ', ' Api_  Account ' = ' xxxx ', ' api_password ' = ' xxxxx ',); Private $alidayu _config = Array (//Channel 2 configuration item ' Api_key ' = ' xxxx ', ' api_id ' = ' xxxxx ', ' api_send_url ' = ' xxxxx '  ',  );      Public function __construct ($type =1) {switch ($type) {case Self::lanchuang: $this->type = $type;      Case Self::alidayu: $this->type = $type;    Default: $this->type = false; }}//Outbound Send method Public function sendsms ($mobile, $msg) {switch ($this->type) {case Self::lanchuang:re      Turn $this->_SENDCL ($mobile, $msg);      Case Self::alidayu:return $this->_sendal ($mobile, $msg);    Default:return false; }}//Channel 1 Send method Private function _SENDCL ($mobile, $msg, $needstatus = ' false ', $extno = ') {$postArr = Array (' account ' = = $this->chuanglan_config[' api_account '), ' pswd ' = $this->chuanglan_config[ ' Api_password ', ' msg ' = $msg, ' mobile ' = $mobile, ' needstatus ' + $needstatus, ' Extno ' =&G T    $EXTNO);    $result = $this->_curlpost ($this->chuanglan_config[' Api_send_url '), $POSTARR);    $result = $this->_execresult ($result); return $result [1] = = 0?  true: $result [1]; }//Channel 2 Send method 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 public methods/** * Process return value \ r \ n Split * */Private Function _execresult ($result) {$result =preg_split ("/[, \r\n]/", $resuLT);  return $result;    }/** * Process return value JSON * */Private Function _jsonresult ($result) {$result =json_decode ($result, true);  return $result; /** * Send HTTP request via Curl * @param string $url//Request URL * @param array $postFields//Request parameter * @return Mixed */Priva    Te 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 Send $res = $ob->sendsms (' xxxxx ', ' xxxxxx '); Var_dump ($res);

A simple distribution enables the free switching of multiple channels,

Because it is simply sent, there is no further abstraction ~

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.