在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.

   }

}

用PHP做到这一点的最佳方法是什么?


繁星coding
浏览 767回答 3
3回答

阿波罗的战车

Kris的解决方案确实很棒,但我发现工厂和流利的风格更好地融合在一起:<?phpclass Student{&nbsp; &nbsp; protected $firstName;&nbsp; &nbsp; protected $lastName;&nbsp; &nbsp; // etc.&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Constructor&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function __construct() {&nbsp; &nbsp; &nbsp; &nbsp; // allocate your stuff&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Static constructor / factory&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static function create() {&nbsp; &nbsp; &nbsp; &nbsp; $instance = new self();&nbsp; &nbsp; &nbsp; &nbsp; return $instance;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* FirstName setter - fluent style&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function setFirstName( $firstName) {&nbsp; &nbsp; &nbsp; &nbsp; $this->firstName = $firstName;&nbsp; &nbsp; &nbsp; &nbsp; return $this;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* LastName setter - fluent style&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function setLastName( $lastName) {&nbsp; &nbsp; &nbsp; &nbsp; $this->lastName = $lastName;&nbsp; &nbsp; &nbsp; &nbsp; return $this;&nbsp; &nbsp; }}// create instance$student= Student::create()->setFirstName("John")->setLastName("Doe");// see resultvar_dump($student);?>

噜噜哒

我可能会做这样的事情:<?phpclass Student{&nbsp; &nbsp; public function __construct() {&nbsp; &nbsp; &nbsp; &nbsp; // allocate your stuff&nbsp; &nbsp; }&nbsp; &nbsp; public static function withID( $id ) {&nbsp; &nbsp; &nbsp; &nbsp; $instance = new self();&nbsp; &nbsp; &nbsp; &nbsp; $instance->loadByID( $id );&nbsp; &nbsp; &nbsp; &nbsp; return $instance;&nbsp; &nbsp; }&nbsp; &nbsp; public static function withRow( array $row ) {&nbsp; &nbsp; &nbsp; &nbsp; $instance = new self();&nbsp; &nbsp; &nbsp; &nbsp; $instance->fill( $row );&nbsp; &nbsp; &nbsp; &nbsp; return $instance;&nbsp; &nbsp; }&nbsp; &nbsp; protected function loadByID( $id ) {&nbsp; &nbsp; &nbsp; &nbsp; // do query&nbsp; &nbsp; &nbsp; &nbsp; $row = my_awesome_db_access_stuff( $id );&nbsp; &nbsp; &nbsp; &nbsp; $this->fill( $row );&nbsp; &nbsp; }&nbsp; &nbsp; protected function fill( array $row ) {&nbsp; &nbsp; &nbsp; &nbsp; // fill all properties from array&nbsp; &nbsp; }}?>然后,如果我想要一个我知道ID的学生:$student = Student::withID( $id );或者,如果我有数据库行的数组:$student = Student::withRow( $row );从技术上讲,您不是在构建多个构造函数,而只是在构建静态辅助方法,而是通过这种方式避免在构造函数中使用大量意大利面条式代码。

四季花海

PHP是一种动态语言,因此您不能重载方法。您必须像这样检查参数的类型:class Student&nbsp;{&nbsp; &nbsp;protected $id;&nbsp; &nbsp;protected $name;&nbsp; &nbsp;// etc.&nbsp; &nbsp;public function __construct($idOrRow){&nbsp; &nbsp; if(is_int($idOrRow))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $this->id = $idOrRow;&nbsp; &nbsp; &nbsp; &nbsp; // other members are still uninitialized&nbsp; &nbsp; }&nbsp; &nbsp; else if(is_array($idOrRow))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;$this->id = $idOrRow->id;&nbsp; &nbsp; &nbsp; &nbsp;$this->name = $idOrRow->name;&nbsp; &nbsp; &nbsp; &nbsp;// etc.&nbsp;&nbsp;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP