0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Laravel】親クラスのコンストラクターを呼び出す

Posted at

__constructorメソッドを記述してた時に気づきがあったので、備忘録としてメモします。

  • 子クラスを作成して__constructorを定義した場合、親クラスの__constructorは実行されず無視される
ParentHoge.php
class ParentHoge
{
	public function __constructor()
	{
		// 処理
	}
}
ChildHoge.php
class ChildHoge extend ParentHoge
{
	public function __constructor()
	{
		// 継承元(ParentHogeクラス)の__constructorは実行されず無視される
		// 処理
	}
}
  • もし親クラスの__constructorも実行したいなら、parent::__constructorを記述する
class ChildHoge extend ParentHoge
{
	public function __constructor()
	{
		parent::__constructor;
		// 処理
	}
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?