2018年7月26日 星期四

php 取得請求的方法

class G {

    public $NOW_TIME;
    public $REQUEST_METHOD;
    public $IS_GET;
    public $IS_POST;
    public $IS_PUT;
    public $IS_DELETE;
    public $IS_AJAX;

    public function __construct() {
        $this->NOW_TIME = $_SERVER['REQUEST_TIME'];
        $this->REQUEST_METHOD = $_SERVER['REQUEST_METHOD'];
        $this->IS_GET = ($this->REQUEST_METHOD == 'GET' ? true : false);
        $this->IS_POST = ($this->REQUEST_METHOD == 'POST' ? true : false);
        $this->IS_PUT = ($this->REQUEST_METHOD == 'PUT' ? true : false);
        $this->IS_DELETE = ($this->REQUEST_METHOD == 'DELETE' ? true : false);

        $ajax_var = 'ajax';
        $postAjax = isset($_POST[$ajax_var]) && $_POST[$ajax_var] == 1 ? true : false;
        $getAjax = isset($_POST[$ajax_var]) && $_POST[$ajax_var] == 1 ? true : false;
        $commonAjax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false;


        $this->IS_AJAX = ($commonAjax || $postAjax || $getAjax) ? true : false;

        }

}

2018年7月25日 星期三

php 讓 container 注入模組的方式

class Container {

    public $name = 'a';
    public $age = 19;
    public $load;

    public function __construct() {
        $this->load = new Loader($this);
    }

    public function getName() {
        printf('<p>%s</p>', $this->name);
    }

}

// 注入模組
class Loader {
    public $container;

    public function __construct($container) {
        $this->container = $container;
    }

    public function __get($name) {

        if (isset($this->container->$name)) {
            return $this->container->$name;
        }

    }

}

$a = new Container();

$load = $a->load;
var_dump($load->name);

2018年7月20日 星期五

php 取得最上層的 className

function getParentClassName($o) {
    $res = $o;

    while (TRUE) {

        $ref = new ReflectionClass($res);
        $r = $ref->getParentClass();

        if ($r) {
            $ref = $r;
        } else {
            break;
        }
        // className
        $res = $ref->getName();
    }

    return $res;
}

2018年7月9日 星期一

javascript.....判斷 global

/** Detect free variable `global` from Node.js. */
  var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;

  /** Detect free variable `self`. */
  var freeSelf = typeof self == 'object' && self && self.Object === Object && self;

  /** Used as a reference to the global object. */
  var root = freeGlobal || freeSelf || Function('return this')();
---------------------------------------------------------------------------------------------------
 // node.js

if (typeof exports != 'undefined' && !exports.nodeType) {
        if (typeof module != 'undefined' && !module.nodeType && module.exports) {
            exports = module.exports = _;
        }
        exports._ = _;
    } else {
        root._ = _;
    }






---------------------------------------------------------------------------------------------------
// node.js

  /** Detect free variable `exports`. */
  var freeExports = typeof exports == 'object' && exports && !exports.nodeType && exports;

  /** Detect free variable `module`. */
  var freeModule = freeExports && typeof module == 'object' && module && !module.nodeType && module;

  /** Detect the popular CommonJS extension `module.exports`. */
  var moduleExports = freeModule && freeModule.exports === freeExports;

  /** Detect free variable `process` from Node.js. */
  var freeProcess = moduleExports && freeGlobal.process;

  /** Used to access faster Node.js helpers. */
  var nodeUtil = (function() {
    try {
      // Use `util.types` for Node.js 10+.
      var types = freeModule && freeModule.require && freeModule.require('util').types;

      if (types) {
        return types;
      }

      // Legacy `process.binding('util')` for Node.js < 10.
      return freeProcess && freeProcess.binding && freeProcess.binding('util');
    } catch (e) {}
  }());
---------------------------------------------------

var root = this
// AMD / RequireJS
if (typeof define !== 'undefined' && define.amd) {
    define([], function () {
        return async;
    });
}
// Node.js
else if (typeof module !== 'undefined' && module.exports) {
    module.exports = async;
}
// included directly via <script> tag
else {
    root.async = async;
}