svn: /phpdoc/ja/trunk/language/oop5/ decon.xml
[email protected] (Yoshinari Takaoka)
| Newsgroups | php.doc.ja |
|---|---|
| Message-ID | <[email protected]> |
mumumu Sun, 22 Nov 2020 03:01:48 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=351484
Log:
[status:ready] Update constructor documentation to include constructor promotion.
Changed paths:
U phpdoc/ja/trunk/language/oop5/decon.xml
svn-diffs-351484.txt
(text/x-diff, 11.1 KB)
Modified: phpdoc/ja/trunk/language/oop5/decon.xml
===================================================================
--- phpdoc/ja/trunk/language/oop5/decon.xml 2020-11-22 02:32:06 UTC (rev 351483)
+++ phpdoc/ja/trunk/language/oop5/decon.xml 2020-11-22 03:01:48 UTC (rev 351484)
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
-<!-- EN-Revision: 351474 Maintainer: hirokawa Status: working -->
+<!-- EN-Revision: 351474 Maintainer: hirokawa Status: ready -->
<!-- CREDITS: shimooka,mumumu -->
<sect1 xml:id="language.oop5.decon" xmlns="http://docbook.org/ns/docbook">
@@ -102,37 +102,40 @@
]]>
</programlisting>
</example>
- <!-- to be translated -->
<para>
- If a class has no constructor, or the constructor has no required arguments, the parentheses
- may be omitted.
+ クラスにコンストラクタが存在しない場合、あるいは、コンストラクタに必須の引数がない場合、
+ 括弧は省略できます。
</para>
<sect3>
- <title>Old-style constructors</title>
+ <title>古いスタイルのコンストラクタ</title>
<para>
- Prior to PHP 8.0.0, classes in the global namespace will interpret a method named
- the same as the class as an old-style constructor. That syntax is deprecated,
- and will result in an <constant>E_DEPRECATED</constant> error but still call that function as a constructor.
- If both <link linkend="object.construct">__construct()</link> and a same-name method are
- defined, <link linkend="object.construct">__construct()</link> will be called.
+ PHP 8.0.0 より前のバージョンでは、グローバル名前空間にあるクラスは、
+ クラス名と同じ名前のメソッドが古いスタイルのコンストラクタとして解釈されます。
+ この文法は推奨されておらず、<constant>E_DEPRECATED</constant> が発生するものの
+ まだこの関数をコンストラクタとして呼び出すことが出来ます。
+ <link linkend="object.construct">__construct()</link>
+ とクラス名と同じ名前のメソッドが両方定義されていた場合は、
+ <link linkend="object.construct">__construct()</link> がコンストラクタとして呼び出されます。
</para>
<para>
- In namespaced classes, or any class as of PHP 8.0.0, a method named
- the same as the class never has any special meaning.
+ 名前空間の中に存在するクラスについては、
+ PHP 8.0.0 以降では、
+ クラス名と同じ名前のメソッドはなんの意味も持ちません。
</para>
- <para>Always use <link linkend="object.construct">__construct()</link> in new code.
+ <para>新しいコードでは、常に <link linkend="object.construct">__construct()</link> を使うようにしましょう。
</para>
</sect3>
<sect3 xml:id="language.oop5.decon.constructor.promotion">
- <title>Constructor Promotion</title>
+ <title>コンストラクタのプロモーション</title>
<para>
- As of PHP 8.0.0, constructor parameters may also be promoted to correspond to an
- object property. It is very common for constructor parameters to be assigned to
- a property in the constructor but otherwise not operated upon. Constructor promotion
- provides a short-hand for that use case. The example above could be rewritten as the following.
+ PHP 8.0.0 以降では、コンストラクタの引数を
+ 対応するオブジェクトのプロパティに昇格させることもできます。
+ コンストラクタの引数をプロパティに代入し、それ以外の操作を行わないことはよくあることです。
+ コンストラクタのプロモーションは、こういった場合の短縮記法を提供します。
+ 上の例は、次のように書き直すことが出来ます。
</para>
<example>
- <title>Using constructor property promotion</title>
+ <title>コンストラクタのプロモーションを使う</title>
<programlisting role="php">
<![CDATA[
<?php
@@ -144,40 +147,46 @@
</programlisting>
</example>
<para>
- When a constructor argument includes a visibility modifier, PHP will interpret it as
- both an object property and a constructor argument, and assign the argument value to
- the property. The constructor body may then be empty or may contain other statements.
- Any additional statements will be executed after the argument values have been assigned
- to the corresponding properties.
+ コンストラクタの引数に、可視性の修飾子が含まれている場合、
+ PHP はそれをオブジェクトのプロパティ、かつコンストラクタの引数であると解釈します。
+ そして、その引数の値をプロパティに代入します。
+ コンストラクタの本体は空にすることもできますし、
+ 他の文を含めることも出来ます。
+ 引数の値が対応するプロパティに代入された後、
+ 追加の文が実行されます。
</para>
<para>
- Not all arguments need to be promoted. It is possible to mix and match promoted and not-promoted
- arguments, in any order. Promoted arguments have no impact on code calling the constructor.
+ 全ての引数をプロパティに昇格させる必要はありません。
+ 昇格させる引数と、させな引数を混ぜることもできます。
+ プロパティに昇格した引数は、コンストラクタの呼び出しコードになんの影響も与えません。
</para>
<note>
<para>
- Object properties may not be typed <type>callable</type> due to engine ambiguity that would
- introduce. Promoted arguments, therefore, may not be typed <type>callable</type> either. Any
- other <link linkend="language.types.declarations">type declaration</link> is permitted, however.
+ PHP のエンジンが曖昧になってしまうため、
+ オブジェクトのプロパティは、<type>callable</type> 型にすることは出来ません。
+ そのため、昇格させる引数も <type>callable</type> 型にはできません。
+ しかし、それ以外の
+ <link linkend="language.types.declarations">型宣言</link> は許されています。
</para>
</note>
<note>
<para>
<!-- This should be linked once attributes are documented. -->
- Attributes placed on a promoted constructor argument will be replicated to both the property
- and argument.
+ 昇格したコンストラクタの引数に指定されたアトリビュートは、
+ プロパティと引数の両方にコピーされます。
</para>
</note>
</sect3>
<sect3 xml:id="language.oop5.decon.constructor.static">
- <title>Static creation methods</title>
+ <title>静的な生成メソッド</title>
<para>
- PHP only supports a single constructor per class. In some cases, however, it may be
- desirable to allow an object to be constructed in different ways with different inputs.
- The recommended way to do so is by using static methods as constructor wrappers.
+ PHP は、クラスひとつにつき、コンストラクタをひとつだけサポートしています。
+ しかし、場合によっては異なる入力を使い、
+ 異なるやり方でオブジェクトを生成させるのが望ましい場合もあります。
+ その場合におすすめなのが、静的メソッドをコンストラクタのラッパーとして使うことです。
</para>
<example>
- <title>Using static creation methods</title>
+ <title>静的な生成メソッドを使う</title>
<programlisting role="php">
<![CDATA[
<?php
@@ -218,27 +227,29 @@
</programlisting>
</example>
<para>
- The constructor may be made private or protected to prevent it from being called externally.
- If so, only a static method will be able to instantiate the class. Because they are in the
- same class definition they have access to private methods, even if not of the same object
- instance. The private constructor is optional and may or may not make sense depending on
- the use case..
+ コンストラクタは外部から呼ばれることを防ぐため、private または protected にしておきます。
+ この場合、静的メソッドだけがクラスをインスタンス化するのに使えます。
+ なぜなら、静的メソッドは、同じオブジェクトのインスタンスでなくとも、
+ private なメソッドにアクセスできる同じクラスのメソッド定義として存在するからです。
+ コンストラクタを private にすることはオプションです。使い方によっては意味がないかもしれません...
</para>
<para>
- The three public static methods then demonstrate different ways of instantiating the object.
+ 上の public な静的メソッドは、オブジェクトをインスタンス化する3つの異なるやり方を示しています。
</para>
<simplelist>
- <member><code>fromBasicData()</code> takes the exact parameters that are needed, then creates the
- object by calling the constructor and returning the result.</member>
- <member><code>fromJson()</code> accepts a JSON string and does some pre-processing on it itself
- to convert it into the format desired by the constructor. It then returns the new object.</member>
- <member><code>fromXml()</code> accepts an XML string, preprocesses it, and then creates a bare
- object. The constructor is still called, but as all of the parameters are optional the method
- skips them. It then assigns values to the object properties directly before returning the result.</member>
+ <member><code>fromBasicData()</code> は、必要となる必須の引数を取り、
+ コンストラクタを呼ぶことでオブジェクトを生成し、その結果を返します。</member>
+ <member><code>fromJson()</code> は、JSON文字列を受け取り、
+ コンストラクタで必要なフォーマットに変換するための前処理を行います。
+ その上で、新しいオブジェクトを返します。</member>
+ <member><code>fromXml()</code> は、XML文字列を受け取り、前処理をします。
+ そして生のオブジェクトを生成します。コンストラクタは呼び出されていますが、
+ 全ての引数はオプションなので、その実行はスキップされます。
+ 結果を返す前に、オブジェクトのプロパティに直接値を代入しています。</member>
</simplelist>
<para>
- In all three cases, the <code>static</code> keyword is translated into the name of the class the code is in.
- In this case, <code>Product</code>.
+ 上の3つの場合全てで、<code>static</code> キーワードはコードが存在するクラスの名前として解釈されます。
+ この場合は <code>Product</code> です。
</para>
</sect3>
</sect2>