初始化多个构造函数的最快方法

时间:2016-11-01 22:31:25

标签: php oop constructor

让我们假设我有三个类,每个类都有自己的构造函数。 我想在一个C类中初始化所有这些,所以我没有为每个类创建很多对象,我应该在每个类中使用class A { private $a; public function __construct($a) { echo $this->a = $a; } } class B extends A { private $b; public function __construct($a,$b) { echo $this->b = $b; parent::__construct($a); } } class C extends B { private $c; public function __construct($a,$b,$c) { echo $this->c = $c; parent::__construct($a,$b); } } 作为示例!有更好的方法吗?

function cache
  (target: Object,
  propertyKey: string,
  // Likely we can do better than <any> here -- <Function<any>> maybe?
  descriptor: TypedPropertyDescriptor<any>) 
{
  let cacheMap = new Map();
  let wrappedFn = descriptor.value;

  descriptor.value = function (...args: any[]) {
    if (cacheMap.has(args)) {
        console.log("Short-circuiting with result: " + cacheMap.get(args));
        return cacheMap.get(args);
    }
    let result = wrappedFn.apply(this, args);
    cacheMap.set(args, result);
    console.log("cacheMap %o", cacheMap);
    return result;
  }

  return descriptor;
}

0 个答案:

没有答案