2

我知道 OOPS 中的以下类型的构造函数:

  • 参数化
  • 默认
  • 复制

但我不确定php是否支持所有这些。php支持的构造函数有哪些类型

4

1 回答 1

2

PHP 支持所有这些:

class A {
    // default is a build-in non-parametrized one

    public function __construct(/* arguments */){
        // parametrized
    }

    public function __clone(){
        // copy
    }
}

// if __construct() is not declared, then uses default one:
$a = new A;

// if __construct() is declared, then uses parametrized one:
$a = new A(/* arguments */);

// if __clone() is declared, then uses copy one:
$b = clone $a;

参考:

  1. 构造函数和析构函数
  2. 对象克隆
于 2013-09-29T09:27:04.380 回答