PHPではメソッドのオーバーロードができない

PHPではメソッドのオーバーロードができない

PHPではメソッドのオーバーロードができない。 下記コードはエラーになる。 class Foo { private $_file_path = null; function __construct () { } function __construct ($file_path) { $this->_file_path = $file_path; } }

対策1 連想配列を引数に取る class Foo { private $_file_path = null; function __construct ($args) { $this->_file_path = $args['file']; } }

対策2 引数のデフォルト値を使う class Foo { private $_file_path = null; function __construct ($_file_path=null) { $this->_file_path = $_file_path; } }

Page Top