Skip to content

「プロパティフック」の翻訳 #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions language/constants.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 5a945dfef09a9595c83855cdb858ffa4a96af305 Maintainer: hirokawa Status: ready -->
<!-- EN-Revision: 9418ae19ae6ab1d3f1536db986830362b207b1d6 Maintainer: hirokawa Status: ready -->
<!-- CREDITS: takagi,mumumu -->

<chapter xml:id="language.constants" xmlns="http://docbook.org/ns/docbook">
Expand Down Expand Up @@ -35,7 +35,6 @@
この場合の定数の値は <function>constant</function> 関数によってのみ取得できますが、
このような使い方は推奨できません。
</para>

&tip.userlandnaming;
<para>
<!-- TODO Move into syntax section? -->
Expand Down Expand Up @@ -69,6 +68,7 @@ define("__FOO__", "something");
(0x80-0xff)のASCII文字を指します。
</simpara>
</note>

<simpara>
&link.superglobals;と同様に定数のスコープはグローバルです。
つまり、スコープによらずスクリプトの中ではどこでも定数に
Expand Down Expand Up @@ -262,7 +262,7 @@ echo ANIMALS[1]; // 出力は "cat"
<sect1 xml:id="language.constants.magic">
<title>マジック定数</title>
<para>
使われる場所によって値が変化する定数(マジック定数)が 9 つあります
使われる場所によって値が変化する定数(マジック定数)がいくつかあります
例えば、<constant>__LINE__</constant>はスクリプト上において
使われる行によって値が変化します。
これらの
Expand Down Expand Up @@ -315,7 +315,7 @@ echo ANIMALS[1]; // 出力は "cat"
クラス名。
クラス名には、そのクラスが宣言されている名前空間も含みます
(例 <literal>Foo\Bar</literal>)。
トレイトのメソッド内で __CLASS__ を使うと、
トレイトのメソッド内で <constant>__CLASS__ </constant> を使うと、
そのトレイトを use しているクラスの名前を返します。
</entry>
</row>
Expand All @@ -333,6 +333,14 @@ echo ANIMALS[1]; // 出力は "cat"
クラスのメソッド名。
</entry>
</row>
<row xml:id="constant.property">
<entry><constant>__PROPERTY__</constant></entry>
<entry>
プロパティ名。
<link linkend="language.oop5.property-hooks">プロパティフック</link>
の内部でのみ使用可能です。
</entry>
</row>
<row xml:id="constant.namespace">
<entry><constant>__NAMESPACE__</constant></entry>
<entry>
Expand Down
3 changes: 2 additions & 1 deletion language/oop5.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: c0fa5077c8862405942d8aac7360c5169558b59b Maintainer: hirokawa Status: ready -->
<!-- EN-Revision: f9dabdea3f92ea77cf5077020a3e945951fdc9db Maintainer: hirokawa Status: ready -->
<!-- CREDITS: mumumu -->

<chapter xml:id="language.oop5" xmlns="http://docbook.org/ns/docbook">
Expand Down Expand Up @@ -29,6 +29,7 @@

&language.oop5.basic;
&language.oop5.properties;
&language.oop5.property-hooks;
&language.oop5.constants;
&language.oop5.autoload;
&language.oop5.decon;
Expand Down
137 changes: 115 additions & 22 deletions language/oop5/abstract.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 9ee9eccf455188ab6eb352194eb6f9eb99e15606 Maintainer: hirokawa Status: ready -->
<!-- EN-Revision: 984188eb6941dc419f49eed6c1a55a6b749a823d Maintainer: hirokawa Status: ready -->
<!-- CREDITS: shimooka -->

<sect1 xml:id="language.oop5.abstract" xmlns="http://docbook.org/ns/docbook">
<title>クラスの抽象化</title>

<para>
PHP には、抽象クラスと抽象メソッドの機能があります。
abstract として定義されたクラスのインスタンスを生成することはできません。
1つ以上の抽象メソッドを含む全てのクラスもまた抽象クラスとなります。
abstract として定義されたメソッドは、そのメソッドのシグネチャを宣言するのみで、
実装を定義することはできません。
PHP には、抽象クラス、抽象メソッド、抽象プロパティがあります。
abstract として定義された抽象クラスのインスタンスを生成することはできず、
1つ以上の抽象メソッドや抽象プロパティを含むクラスは抽象クラスでなければいけません。
abstract として定義されたメソッドは、そのメソッドのシグネチャと public または protected のアクセス権を宣言するのみで、
実装を定義することはできません。抽象プロパティは、
<literal>get</literal> や <literal>set</literal> の要件を宣言することができ、
実装はどちらか一方に対してのみ行えます。両方同時に実装することはできません。
</para>

<para>
Expand All @@ -21,52 +23,69 @@
<link linkend="language.oop.lsp">シグネチャの互換性に関するルール</link> に従わなければいけません。
</para>

<simpara>
PHP 8.4 から、抽象クラスは public または protected の抽象プロパティを宣言できるようになりました。
protected な抽象プロパティは、protected または public のスコープから読み書き可能なプロパティにより
要件が満たされます。
</simpara>
<simpara>
抽象プロパティは、通常のプロパティによって、または
必要な操作に対応した <link linkend="language.oop5.property-hooks">フック</link> を定義したプロパティにより要件が満たされます。
</simpara>

<example>
<title>抽象クラスの例</title>
<title>抽象メソッドの例</title>
<programlisting role="php">
<![CDATA[
<?php

abstract class AbstractClass
{
// 拡張クラスにこのメソッドの定義を強制する
abstract protected function getValue();
abstract protected function prefixValue($prefix);

// Common method
public function printOut() {
public function printOut()
{
print $this->getValue() . "\n";
}
}

class ConcreteClass1 extends AbstractClass
{
protected function getValue() {
protected function getValue()
{
return "ConcreteClass1";
}

public function prefixValue($prefix) {
public function prefixValue($prefix)
{
return "{$prefix}ConcreteClass1";
}
}

class ConcreteClass2 extends AbstractClass
{
public function getValue() {
public function getValue()
{
return "ConcreteClass2";
}

public function prefixValue($prefix) {
public function prefixValue($prefix)
{
return "{$prefix}ConcreteClass2";
}
}

$class1 = new ConcreteClass1;
$class1 = new ConcreteClass1();
$class1->printOut();
echo $class1->prefixValue('FOO_') ."\n";
echo $class1->prefixValue('FOO_'), "\n";

$class2 = new ConcreteClass2;
$class2 = new ConcreteClass2();
$class2->printOut();
echo $class2->prefixValue('FOO_') ."\n";
echo $class2->prefixValue('FOO_'), "\n";

?>
]]>
</programlisting>
Expand All @@ -82,36 +101,38 @@ FOO_ConcreteClass2
</example>

<example>
<title>抽象クラスの例</title>
<title>抽象メソッドの例</title>
<programlisting role="php">
<![CDATA[
<?php

abstract class AbstractClass
{
// 抽象メソッドでは、必須の引数だけを定義しています
abstract protected function prefixName($name);

}

class ConcreteClass extends AbstractClass
{

// 子クラスでは、親のシグネチャにないオプション引数を定義することもあるでしょう
public function prefixName($name, $separator = ".") {
public function prefixName($name, $separator = ".")
{
if ($name == "Pacman") {
$prefix = "Mr";
} elseif ($name == "Pacwoman") {
$prefix = "Mrs";
} else {
$prefix = "";
}

return "{$prefix}{$separator} {$name}";
}
}

$class = new ConcreteClass;
$class = new ConcreteClass();
echo $class->prefixName("Pacman"), "\n";
echo $class->prefixName("Pacwoman"), "\n";

?>
]]>
</programlisting>
Expand All @@ -123,9 +144,81 @@ Mrs. Pacwoman
]]>
</screen>
</example>
<example>
<title>抽象プロパティの例</title>
<programlisting role="php">
<![CDATA[
<?php

</sect1>
abstract class A
{
// 継承するクラスは、public に読み取り可能なプロパティを持たなければなりません
abstract public string $readable {
get;
}

// 継承するクラスは、protected または public に書き込み可能なプロパティを持たなければなりません
abstract protected string $writeable {
set;
}

// 継承するクラスは、protected または public で読み書き可能なプロパティを持たなければなりません
abstract protected string $both {
get;
set;
}
}

class C extends A
{
// 要件を満たし、さらに書き込みも可能にしているため有効です
public string $readable;

// public に読み取り可能でないため、要件を満たしません
protected string $readable;

// 要件を正確に満たしているため有効です
// protected のスコープからのみ書き込みが可能です
protected string $writeable {
set => $value;
}

// protected から public にアクセス権を拡張しており、問題ありません
public string $both;
}

?>
]]>
</programlisting>
</example>
<simpara>
抽象プロパティにはフックを実装できます。
前の例のように、<literal>get</literal> または <literal>set</literal> のどちらかを、定義せず宣言のみ行います。
</simpara>
<example>
<title>抽象プロパティの例</title>
<programlisting role="php">
<![CDATA[
<?php

abstract class A
{
// set のデフォルト実装(オーバーライド可能)を提供し、
// 子クラスが get を実装するよう要求しています
abstract public string $foo {
get;

set {
$this->foo = $value;
}
}
}

?>
]]>
</programlisting>
</example>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
Expand Down
Loading