1
0
mirror of https://github.com/php/doc-ja.git synced 2026-03-24 07:02:08 +01:00
Files
archived-doc-ja/language/oop5/final.xml
Louis-Arnaud 2d259303c5 表記揺れ修正: OOP の文脈で 上書き → オーバーライド に統一 (#326)
* 表記揺れ修正: OOP の文脈で 上書き → オーバーライド に統一

Closes #26

* magic.xml, property-hooks.xml: 上書き に戻す(OOP継承以外の文脈)

---------

Co-authored-by: KentarouTakeda <4785040+KentarouTakeda@users.noreply.github.com>
2026-02-25 22:50:46 +09:00

133 lines
3.3 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 16f66c05a4060a7d673ae1c70b656d65009407b0 Maintainer: hirokawa Status: ready -->
<sect1 xml:id="language.oop5.final" xmlns="http://docbook.org/ns/docbook">
<title>finalキーワード</title>
<para>
キーワード <literal>final</literal>
を前に付けて定義されたメソッドやプロパティ、定数は、子クラスからオーバーライドできません。
クラス自体がfinalと定義された場合には、このクラスを拡張することはできません。
</para>
<para>
<example>
<title>finalメソッドの例</title>
<programlisting role="php">
<![CDATA[
<?php
class BaseClass {
public function test() {
echo "BaseClass::test() called\n";
}
final public function moreTesting() {
echo "BaseClass::moreTesting() called\n";
}
}
class ChildClass extends BaseClass {
public function moreTesting() {
echo "ChildClass::moreTesting() called\n";
}
}
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
?>
]]>
</programlisting>
</example>
</para>
<para>
<example>
<title>finalクラスの例</title>
<programlisting role="php">
<![CDATA[
<?php
final class BaseClass {
public function test() {
echo "BaseClass::test() called\n";
}
// As the class is already final, the final keyword is redundant
final public function moreTesting() {
echo "BaseClass::moreTesting() called\n";
}
}
class ChildClass extends BaseClass {
}
// Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass)
?>
]]>
</programlisting>
</example>
</para>
<example>
<title>finalプロパティの例(PHP 8.4.0 以降)</title>
<programlisting role="php">
<![CDATA[
<?php
class BaseClass {
final protected string $test;
}
class ChildClass extends BaseClass {
public string $test;
}
// Results in Fatal error: Cannot override final property BaseClass::$test
?>
]]>
</programlisting>
</example>
<example xml:id="language.oop5.final.example.php81">
<title>final定数の例(PHP 8.1.0 以降)</title>
<programlisting role="php">
<![CDATA[
<?php
class Foo
{
final public const X = "foo";
}
class Bar extends Foo
{
public const X = "bar";
}
// Fatal error: Bar::X cannot override final constant Foo::X
?>
]]>
</programlisting>
</example>
<note>
<simpara>
PHP 8.0.0 以降は、private メソッドを final として宣言できるのは<link linkend="language.oop5.decon.constructor">コンストラクタ</link>だけになりました。
</simpara>
</note>
<note>
<simpara>
<link linkend="language.oop5.visibility-members-aviz"><literal>private(set)</literal></link> として宣言されたプロパティは暗黙的に<literal>final</literal>になります。
</simpara>
</note>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->