From 43414dc4ab6b609ed71edb3e7baa033cc68049b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=A6=E7=94=B0=20=E6=86=B2=E5=A4=AA=E9=83=8E?= Date: Wed, 3 Jun 2026 19:05:51 +0900 Subject: [PATCH] =?UTF-8?q?=E8=A8=80=E8=AA=9E=E3=83=AA=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=83=AC=E3=83=B3=E3=82=B9=E3=81=AE=E3=82=B3=E3=83=BC=E3=83=89?= =?UTF-8?q?=E3=82=B5=E3=83=B3=E3=83=97=E3=83=AB=E3=82=92=E5=AE=9F=E8=A1=8C?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E5=8C=96=EF=BC=888=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=A4=E3=83=AB=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit マニュアル上でコードサンプルを直接実行できるようにする対応(および一部の DocBook 5.2 マークアップ移行)に追随しました。変更はコードサンプルと構造属性の同期が中心で、closure.xml を除き訳文の変更はありません。 ### language(4件) - language/enumerations.xml — enum セクションのサンプルを実行可能化(訳文変更なし) 1. php/doc-en@1eb67bea30 - language/variables.xml — variables セクションのサンプルを実行可能化(訳文変更なし) 1. php/doc-en@64007e9f6b - language/references.xml — references セクションのサンプルを実行可能化(訳文変更なし) 1. php/doc-en@50e6f42c83 - language/attributes.xml — attributes セクションのサンプルを実行可能化(訳文変更なし) 1. php/doc-en@50e6f42c83 ### language/predefined(4件) - language/predefined/iteratoraggregate.xml — サンプルの実行可能化+コード例の更新(訳文変更なし) 1. php/doc-en@4d17b7b494 2. php/doc-en@4b06b2d5c3 - language/predefined/arrayaccess.xml — サンプルの実行可能化+出力例の同期(訳文変更なし) 1. php/doc-en@4d17b7b494 2. php/doc-en@4b06b2d5c3 - language/predefined/serializable.xml — サンプルの実行可能化に伴う非推奨警告出力の追加(訳文変更なし) 1. php/doc-en@4d17b7b494 2. php/doc-en@4b06b2d5c3 - language/predefined/closure.xml — Closure オブジェクトが直列化できない旨の note を追加 1. php/doc-en@a97672ab04 --- language/attributes.xml | 39 ++- language/enumerations.xml | 286 ++++++++++++++-------- language/predefined/arrayaccess.xml | 7 +- language/predefined/closure.xml | 14 +- language/predefined/iteratoraggregate.xml | 35 +-- language/predefined/serializable.xml | 3 +- language/references.xml | 89 +++---- language/variables.xml | 130 +++++----- 8 files changed, 349 insertions(+), 254 deletions(-) diff --git a/language/attributes.xml b/language/attributes.xml index 2275a030de..d4531f317a 100644 --- a/language/attributes.xml +++ b/language/attributes.xml @@ -1,8 +1,8 @@ - + - + アトリビュート アトリビュートの概要 @@ -38,7 +38,7 @@ アトリビュートを使い、インターフェイスのオプションのメソッドを実装する - + アトリビュートの文法 - + + + &example.outputs; + + @@ -240,10 +244,8 @@ object(MyAttribute)#3 (1) { ["value"]=> int(1234) } -*/ - ]]> - + @@ -258,6 +260,21 @@ object(MyAttribute)#3 (1) { value = $value; + } +} + +#[MyAttribute(value: 1234)] +class Thing +{ +} function dumpMyAttributeData($reflection) { $attributes = $reflection->getAttributes(MyAttribute::class); @@ -287,7 +304,7 @@ dumpMyAttributeData(new ReflectionClass(Thing::class)); 単純なアトリビュートクラスの例 - + アトリビュートを使える場所を限定するために、ターゲットとなるクラスを作る - + 宣言時にアトリビュートを複数回使えるように、IS_REPEATABLE を使う - + - - + + 列挙型(Enum) 列挙型の概要 @@ -43,10 +43,9 @@ - + ]]> @@ -68,13 +66,21 @@ enum Suit その場合は列挙型の値だけが渡せます。 - + + ]]> - + + 列挙型は、0個以上の @@ -111,21 +117,35 @@ pick_a_card('Spades'); 以下のコードがそれを示しています: - + + +if ($a !== 'Spades') { + print "Suit does not match the string\n"; +} ]]> - + + このことは、列挙型の値は決して @@ -152,15 +172,23 @@ $a !== 'Spades'; // true これは大文字小文字が区別される、case そのものの名前です。 - + + name; // "Spades" と表示 -?> ]]> - + + 列挙型の名前を動的に取得している場合、 @@ -191,10 +219,9 @@ print Suit::Spades->name; 列挙型にスカラー値を定義するには、以下のようにします: - + ]]> @@ -246,15 +272,23 @@ enum Suit: string これは、Backed Enum の定義で指定された値です。 - + + value; // "C" と表示 -?> ]]> - + + value プロパティを強制的に読み取り専用にするため、 @@ -262,16 +296,24 @@ print Suit::Clubs->value; つまり、以下のようなコードはエラーになります: - + + value; -// Error: Cannot acquire reference to property Suit::$value -?> +// Fatal Error: Cannot indirectly modify readonly property Suit::$value ]]> - + + Backed Enum は内部的に @@ -311,23 +353,37 @@ $ref = &$suit->value; それ以外の型については、どちらのモードでも TypeError が発生します。 - + + 'S', + ]; +} -$suit = Suit::from($record['suit']); -// 不正なデータを渡すと、次のようなエラーが発生 -> ValueError: "X" is not a valid scalar value for enum "Suit" -print $suit->value; +$record = get_stuff_from_database(42); +print $record['suit'] . "\n"; $suit = Suit::tryFrom('A') ?? Suit::Spades; // 不正なデータに対しては null を返すので、Suit::Spades が代わりに使われます。 -print $suit->value; -?> +print $suit->value . "\n"; + +$suit = Suit::from('X'); +// 不正なデータを渡すと、次のようなエラーが発生 -> ValueError: "X" is not a valid backing scalar value for enum Suit +print $suit->value . "\n"; ]]> - + + Backed Enum において、手動で from() や @@ -349,10 +405,10 @@ print $suit->value; 実装した列挙型の全ての case で有効です。 - + + color() . "\n"; } paint(Suit::Clubs); // 動作します print Suit::Diamonds->shape(); // "Rectangle" と表示 -?> ]]> - + + 上記の例の Suit の4つのインスタンスは、 @@ -406,10 +462,9 @@ print Suit::Diamonds->shape(); // "Rectangle" と表示 インターフェイスの宣言は依存する型の宣言の直後に置きます。 - + ]]> @@ -461,10 +515,9 @@ enum Suit: string implements Colorful (しかし、以下のコードは実際には動作しません): - + ]]> @@ -519,10 +571,10 @@ final class Suit implements UnitEnum, Colorful コンストラクタの代わりをさせるためです。たとえば、以下のようなものです: - + + + +var_dump(Size::fromLength(50)); ]]> - + + static メソッドには public, private, protected が指定できますが、 @@ -564,10 +618,10 @@ enum Size 列挙型の定数は、以下のように case を参照していても構いません: - + + + +var_dump(Size::Huge); ]]> - + + @@ -594,10 +650,10 @@ enum Size プロパティを持つトレイトの場合、致命的なエラーが発生します。 - + + + +$suit = Suit::Spades; +var_dump($suit->color()); +var_dump($suit->shape()); ]]> - + + @@ -655,10 +715,9 @@ enum Suit implements Colorful 引き続き定数式では不正な操作として扱われます。 - + ]]> @@ -772,16 +830,14 @@ $foo = new Foo(); これらは両方、エラーになります。 - + newInstanceWithoutConstructor() // Error: Cannot instantiate enum Suit -?> ]]> @@ -799,15 +855,32 @@ $horseshoes = (new ReflectionClass(Suit::class))->newInstanceWithoutConstructor( 定義されている全ての case を宣言された順に含めた配列を返します。 - + + +var_dump(Suit::cases()); + +enum SuitBacked: string +{ + case Hearts = 'H'; + case Diamonds = 'D'; + case Clubs = 'C'; + case Spades = 'S'; +} + +var_dump(SuitBacked::cases()); ]]> - + + 列挙型において、手動で cases() @@ -827,17 +900,25 @@ Suit::cases(); これにより、以下のようなコードが動作することが保証されます: - + + ]]> - + + アンシリアライズする際に、 @@ -861,10 +942,10 @@ print serialize(Suit::Hearts); これは、混乱を最小限に抑えるためです。 - + + 5 } */ -?> ]]> - + + @@ -899,10 +980,9 @@ Baz Enum:int { クラスは、メソッドの使い方を約束(契約)するものです: - + ]]> @@ -925,10 +1004,9 @@ function bar(B $b) { 一方で、列挙型は case について契約するものです。メソッドではありません: - + true, }; } - -?> ]]> @@ -955,10 +1031,9 @@ function quux(ErrorCode $errorCode) - + ]]> - + 通常の継承のルールでは、あるクラスの子クラスは親クラスの型チェックを通過します。 @@ -997,10 +1070,9 @@ fot(MoreErrorCode::PEBKAC); 基本的な、値を限定する例 - + ]]> @@ -1035,7 +1106,6 @@ function query($fields, $filter, SortOrder $order = SortOrder::Asc) + +$status = UserStatus::Suspended; +var_dump($status->label()); ]]> @@ -1080,11 +1152,31 @@ enum UserStatus: string 'Pending', + self::Active => 'Active', + self::Suspended => 'Suspended', + self::CanceledByUser => 'Canceled by user', + }; + } +} foreach (UserStatus::cases() as $case) { - printf('\n', $case->value, $case->label()); + printf( + "\n", + htmlentities($case->value), + htmlentities($case->label()) + ); } -?> ]]> diff --git a/language/predefined/arrayaccess.xml b/language/predefined/arrayaccess.xml index ede3146a6e..96f76460ce 100644 --- a/language/predefined/arrayaccess.xml +++ b/language/predefined/arrayaccess.xml @@ -1,6 +1,6 @@ - + @@ -83,7 +83,6 @@ $obj[] = 'Append 1'; $obj[] = 'Append 2'; $obj[] = 'Append 3'; print_r($obj); -?> ]]> &example.outputs.similar; @@ -93,9 +92,9 @@ bool(true) int(2) bool(false) string(7) "A value" -obj Object +Obj Object ( - [container:obj:private] => Array + [container] => Array ( [one] => 1 [three] => 3 diff --git a/language/predefined/closure.xml b/language/predefined/closure.xml index 2a30d6fa47..01070465cd 100644 --- a/language/predefined/closure.xml +++ b/language/predefined/closure.xml @@ -1,6 +1,6 @@ - + @@ -78,6 +78,18 @@ +
+ &reftitle.notes; + + + Closure オブジェクトはシリアライズできません。 + クロージャは束縛された変数や特定の実行コンテキストを保持している可能性があるためです。 + クロージャをシリアライズしようとすると、Exception + がスローされます。 + + +
+ &language.predefined.closure.construct; diff --git a/language/predefined/iteratoraggregate.xml b/language/predefined/iteratoraggregate.xml index 67780ef47c..d773bb557a 100644 --- a/language/predefined/iteratoraggregate.xml +++ b/language/predefined/iteratoraggregate.xml @@ -1,6 +1,6 @@ - + IteratorAggregate インターフェイス @@ -50,19 +50,13 @@ class myData implements IteratorAggregate { - public $property1 = "Public property one"; - public $property2 = "Public property two"; - public $property3 = "Public property three"; - public $property4 = ""; - - public function __construct() - { - $this->property4 = "last property"; - } - public function getIterator(): Traversable { - return new ArrayIterator($this); + return new ArrayIterator([ + "key one" => "item one", + "key two" => "item two", + "key three" => "item three" + ]); } } @@ -72,24 +66,19 @@ foreach ($obj as $key => $value) { var_dump($key, $value); echo "\n"; } - -?> ]]>
&example.outputs.similar; diff --git a/language/predefined/serializable.xml b/language/predefined/serializable.xml index 0652bfbefb..430539c92d 100644 --- a/language/predefined/serializable.xml +++ b/language/predefined/serializable.xml @@ -1,6 +1,6 @@ - + Serializable インターフェイス @@ -94,6 +94,7 @@ var_dump($newobj->getData()); &example.outputs.similar; diff --git a/language/references.xml b/language/references.xml index 7621f4ffe3..f0ce0d4f70 100644 --- a/language/references.xml +++ b/language/references.xml @@ -1,8 +1,8 @@ - + - + リファレンスの説明 @@ -40,13 +40,10 @@ ふたつの変数が同じ内容を指すようにできます。 次の例を考えてみましょう。 - + ]]> @@ -82,8 +79,6 @@ var_dump(array_key_exists('b', $b)); // bool(true) $c = new stdClass(); foo($c->d); var_dump(property_exists($c, 'd')); // bool(true) - -?> ]]>
@@ -93,13 +88,10 @@ var_dump(property_exists($c, 'd')); // bool(true) リファレンスを返す関数でも 同じ構文が使用可能です - + ]]> @@ -122,7 +114,6 @@ $foo =& find_var($bar); ]]> @@ -161,7 +150,6 @@ echo "var2 の値は '$var2'\n"; // var2 の値は 'Example variable' ]]> @@ -186,7 +172,6 @@ echo $ref; // 3 - 配列の最後の要素 +var_dump($a); +var_dump($b); ]]> @@ -217,6 +202,7 @@ $a = 1; $b =& $a; $c = $b; $c = 7; // $c はリファレンスではないので $a や $b の値は変わりません +print "a = {$a}; b = {$b}; c = {$c}\n\n"; /* 配列変数への代入 */ $arr = array(1); @@ -225,8 +211,9 @@ $arr2 = $arr; // これは、リファレンス代入ではありません! $arr2[0]++; /* $a == 2, $arr == array(2) */ /* $arr の内容は、たとえリファレンスでなくても変わります! */ - -?> +print "a = {$a}\n"; +var_dump($arr); +var_dump($arr2); ]]> @@ -245,16 +232,14 @@ $arr2[0]++; +print $a; ]]> @@ -282,18 +267,15 @@ foo($a); これまでに説明したように、リファレンスはポインタではありません。このため、 次の例は期待通りに動作しません。 - + ]]> @@ -325,18 +307,14 @@ foo($bar); +print $a; // $a はここでは 6 です ]]> @@ -367,6 +345,7 @@ foo($a); function foo(&$var) { $var++; + print $var; } function &bar() @@ -376,8 +355,6 @@ function &bar() } foo(bar()); - -?> ]]> @@ -391,7 +368,7 @@ foo(bar()); 他の式は、結果が未定義となるため、リファレンスで渡すべきではありません。 例えば、リファレンスで渡す次の例は、無効です。 - + ]]> @@ -450,8 +425,6 @@ $obj = new Foo(); $myValue = &$obj->getValue(); // $myValue は $obj->value へのリファレンス、つまり 42 となります $obj->value = 2; echo $myValue; // $obj->value の新しい値である 2 を表示します - -?> ]]> @@ -495,14 +468,17 @@ $collection = &collector(); $collection[] = 'foo'; print_r(collector()); -// Array -// ( -// [0] => foo -// ) - -?> ]]> + &example.outputs; + + foo +) +]]> + @@ -515,7 +491,7 @@ print_r(collector()); 返されたリファレンスを、リファレンスを要求する別の関数に渡すには、 この構文を使います。 - + ]]> @@ -554,8 +528,8 @@ array_push(collector(), 'foo'); $a = 1; $b =& $a; unset($a); - -?> +var_dump($a); +var_dump($b); ]]> @@ -587,13 +561,10 @@ unset($a); グローバル変数へのリファレンスを作成したことになります。この意味は、 次の例と同じです。 - + ]]> diff --git a/language/variables.xml b/language/variables.xml index 93dedccebb..6502c7a93a 100644 --- a/language/variables.xml +++ b/language/variables.xml @@ -1,8 +1,9 @@ - + - + 変数 @@ -45,7 +46,7 @@ 有効な変数名 - + ]]> @@ -85,7 +85,6 @@ $4site = 'not yet'; // invalid; starts with a number ${'invalid-name'} = 'bar'; $name = 'invalid-name'; echo ${'invalid-name'}, " ", $$name; -?> ]]> &example.outputs; @@ -124,9 +123,8 @@ bar bar $foo = 'Bob'; // 値'Bob'を$fooに代入する。 $bar = &$foo; // $fooを$barにより参照 $bar = "My name is $bar"; // $barを変更... -echo $bar; -echo $foo; // $fooも変更される。 -?> +echo $bar . PHP_EOL; +echo $foo . PHP_EOL; // $fooも変更される。 ]]> @@ -139,16 +137,37 @@ echo $foo; // $fooも変更される。 + + + + + +// 注意: リファレンスでない値が代入されます。 +$result2 = &test2(); +var_dump($result2); ]]> @@ -173,7 +192,6 @@ $bar = &test(); // 無効。なぜなら、リファレンスで値を返し ]]> &example.outputs; @@ -198,7 +216,7 @@ NULL +var_dump($unset_array); ]]> @@ -267,13 +285,12 @@ $unset_array[] = 'value'; // 警告は発生しません グローバル変数のスコープの例 - + ]]> @@ -297,9 +314,10 @@ $a = 1; // グローバルスコープ function test() { - echo $a; // $a は、ローカルスコープの $a を参照しているので未定義です + var_dump($a); // $a は、ローカルスコープの $a を参照しているので未定義です } -?> + +test(); ]]> @@ -345,7 +363,6 @@ function Sum() Sum(); echo $b; -?> ]]> &example.outputs; @@ -386,7 +403,6 @@ function Sum() Sum(); echo $b; -?> ]]> @@ -403,14 +419,13 @@ echo $b; スーパーグローバルとスコープの例 - + ]]> @@ -440,10 +455,13 @@ function test_superglobal() function test() { $a = 0; - echo $a; + echo $a . PHP_EOL; $a++; } -?> + +test(); +test(); +test(); ]]> @@ -465,10 +483,13 @@ function test() function test() { static $a = 0; - echo $a; + echo $a . PHP_EOL; $a++; } -?> + +test(); +test(); +test(); ]]> @@ -497,13 +518,14 @@ function test() static $count = 0; $count++; - echo $count; + echo $count . PHP_EOL; if ($count < 10) { test(); } $count--; } -?> + +test(); ]]> @@ -517,7 +539,7 @@ function test() static 変数の宣言 - + ]]> @@ -559,7 +580,6 @@ function exampleFunction($input) { // variable does not retain its value. echo exampleFunction('A'); // Outputs: Input: A, Counter: 1 echo exampleFunction('B'); // Outputs: Input: B, Counter: 1 -?> ]]> @@ -596,7 +616,6 @@ var_dump(Foo::counter()); // int(1) var_dump(Foo::counter()); // int(2) var_dump(Bar::counter()); // int(3), PHP 8.1.0 より前のバージョンでは int(1) var_dump(Bar::counter()); // int(4), PHP 8.1.0 より前のバージョンでは int(2) -?> ]]> @@ -638,12 +657,10 @@ test_global_ref(); var_dump($obj); test_global_noref(); var_dump($obj); -?> ]]> - - &example.outputs; + &example.outputs; - + + 類似の動作がstatic命令にも適用されます。 @@ -703,10 +721,8 @@ $still_obj1 = get_instance_ref(); echo "\n"; $obj2 = get_instance_noref(); $still_obj2 = get_instance_noref(); -?> ]]> - この例を実行すると以下の出力となります。 @@ -724,6 +740,7 @@ Static object: object(stdClass)#3 (1) { } +]]> + この例は、static変数にリファレンスを代入した時に @@ -747,7 +764,7 @@ Static object: object(stdClass)#3 (1) { +var_dump($a); ]]> @@ -762,8 +779,9 @@ $a = 'hello'; +var_dump($hello); ]]> @@ -778,8 +796,9 @@ $$a = 'world'; ]]> @@ -792,8 +811,9 @@ echo "$a {$$a}"; ]]> @@ -856,8 +876,6 @@ echo $foo->{$start . $end} . "\n"; $arr = 'arr'; echo $foo->{$arr}[1] . "\n"; - -?> ]]> &example.outputs; @@ -920,12 +938,11 @@ I am B. 簡単なPOST HTMLフォームからのデータにアクセスする - + ]]> @@ -961,7 +978,7 @@ echo $_REQUEST['username']; より複雑なフォーム変数 - + - + +setcookie("MyCookie[foo]", 'Testing 1', time()+3600); +setcookie("MyCookie[bar]", 'Testing 2', time()+3600); ]]> @@ -1083,7 +1099,7 @@ if ($_POST) { <function>setcookie</function>の例 - + ]]> @@ -1107,11 +1122,10 @@ setcookie("Cart[$count]", $item, time()+3600); 通常、PHP はスクリプトに渡された変数の名前を変更しません。しかし、 ドット(ピリオド、終止符)はPHPの変数名で有効な文字ではないというこ とに注意する必要があります。次の例を見てみましょう。 - + ]]> ここで、パーサーは、$varnameという名前の変数の後に