相关疑难解决方法(0)

在PHP中执行多个构造函数的最佳方法

您不能在PHP类中放置两个带有唯一参数签名的__construct函数.我想这样做:

class Student 
{
   protected $id;
   protected $name;
   // etc.

   public function __construct($id){
       $this->id = $id;
      // other members are still uninitialized
   }

   public function __construct($row_from_database){
       $this->id = $row_from_database->id;
       $this->name = $row_from_database->name;
       // etc.
   }
}
Run Code Online (Sandbox Code Playgroud)

在PHP中执行此操作的最佳方法是什么?

php constructor multiple-constructors

329
推荐指数
9
解决办法
17万
查看次数

标签 统计

constructor ×1

multiple-constructors ×1

php ×1