zoukankan      html  css  js  c++  java
  • OOP面向对象形式的初使化配置

    init.php里:

     1 <?php
     2 
     3 use ElemeOpenApiConfigConfig;
     4 
     5 define("BASE_DIR", dirname(__FILE__) . "/");
     6 define("ROOT_DIR", dirname(__FILE__) . "/../");
     7 require BASE_DIR . "../vendor/autoload.php";
     8 require ROOT_DIR . "vendor/autoload.php";
     9 
    10 //此处需要填写对应的参数
    11 $app_key = "";
    12 $app_secret = "";
    13 $sandbox = true;
    14 
    15 $scope = "all";
    16 $callback_url = "";
    17 
    18 
    19 $config = new Config($app_key, $app_secret, $sandbox);

    Config.php里

     1 <?php
     2 
     3 namespace ElemeOpenApiConfig;
     4 
     5 use InvalidArgumentException;
     6 
     7 class Config
     8 {
     9     private $app_key;
    10     private $app_secret;
    11     private $sandbox;
    12 
    13     private $request_url;
    14 
    15     private $log;
    16 
    17     private $default_request_url = "https://open-api.shop.ele.me";
    18     private $default_sandbox_request_url = "https://open-api-sandbox.shop.ele.me";
    19 
    20     public function __construct($app_key, $app_secret, $sandbox)
    21     {
    22         if ($sandbox == false) {
    23             $this->request_url = $this->default_request_url;
    24         } elseif ($sandbox == true) {
    25             $this->request_url = $this->default_sandbox_request_url;
    26         } else {
    27             throw new InvalidArgumentException("the type of sandbox should be a boolean");
    28         }
    29 
    30         if ($app_key == null || $app_key == "") {
    31             throw new InvalidArgumentException("app_key is required");
    32         }
    33 
    34         if ($app_secret == null || $app_secret == "") {
    35             throw new InvalidArgumentException("app_secret is required");
    36         }
    37 
    38         $this->app_key = $app_key;
    39         $this->app_secret = $app_secret;
    40         $this->sandbox = $sandbox;
    41     }
    42 
    43     public function get_app_key()
    44     {
    45         return $this->app_key;
    46     }
    47 
    48     public function get_app_secret()
    49     {
    50         return $this->app_secret;
    51     }
    52 
    53     public function get_request_url()
    54     {
    55         return $this->request_url;
    56     }
    57 
    58     public function set_request_url($request_url)
    59     {
    60         $this->request_url = $request_url;
    61     }
    62 
    63     public function get_log()
    64     {
    65         return $this->log;
    66     }
    67 
    68     public function set_log($log)
    69     {
    70         if (!method_exists($log, "info")) {
    71             throw new InvalidArgumentException("logger need have method 'info($message)'");
    72         }
    73         if (!method_exists($log, "error")) {
    74             throw new InvalidArgumentException("logger need have method 'error($message)'");
    75         }
    76         $this->log = $log;
    77     }
    78 }
    [Haima的博客] http://www.cnblogs.com/haima/
  • 相关阅读:
    Linux 内核优化
    Myeclipse7.5 下载 安装 注冊 注冊码 100%成功
    Nmap命令的29个实用范例
    运维工程师必会的109个Linux命令
    linux下维护服务器之常用命令
    黑客工具软件大全100套
    Sysstat性能监控工具包中20个实用命令
    100个linux常用命令
    28 个 Unix/Linux 的命令行神器
    linux命令合集
  • 原文地址:https://www.cnblogs.com/haima/p/9407365.html
Copyright © 2011-2022 走看看