1
0

More than 1 year has passed since last update.

PHP final定義

Last updated at Posted at 2022-03-11

finalメソッド・final定数

PHPにおいて、finalで定義されたメソッドや定数は子クラスから上書きできない。

<?php
class BaseClass
{
  final function hoge()
  {
    // 処理
  }
}

class ChildClass extends BaseClass
{
  public function hoge() 
  {
    // 上書き処理
  }
}

// Results in Fatal error: Cannot override final method
// finalメソッドを上書きできない

finalクラス

final定義されたクラスは継承することができなくなる。

final class HogeClass
{
  // クラス内容
}

class ChildClass extends HogeClass
{
  // クラス内容
}

// Results in Fatal error: Class ChildClass may not inherit from final class (HogeClass)
// finalクラスは継承できない

参考

PHP: finalキーワード

1
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
1
0