[prev in list] [next in list] [prev in thread] [next in thread]
List: php-doc-cvs
Subject: [DOC-CVS] =?utf-8?q?svn:_/phpdoc/en/trunk/language/oop5/_properties.xml_variance.xml?=
From: Peter_Cowburn <salathe () php ! net>
Date: 2019-12-17 20:24:03
Message-ID: svn-salathe-1576614243-348562-863794136 () svn ! php ! net
[Download RAW message or body]
salathe Tue, 17 Dec 2019 20:24:03 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=348562
Log:
k
Changed paths:
U phpdoc/en/trunk/language/oop5/properties.xml
U phpdoc/en/trunk/language/oop5/variance.xml
Modified: phpdoc/en/trunk/language/oop5/properties.xml
===================================================================
--- phpdoc/en/trunk/language/oop5/properties.xml 2019-12-17 17:49:30 UTC (rev 348561)
+++ phpdoc/en/trunk/language/oop5/properties.xml 2019-12-17 20:24:03 UTC (rev 348562)
@@ -9,11 +9,11 @@
"fields", but for the purposes of this reference we will use
"properties". They are defined by using one of the
keywords <literal>public</literal>, <literal>protected</literal>,
- or <literal>private</literal>, followed by a normal variable
- declaration. This declaration may include an initialization, but
- this initialization must be a constant value--that is, it must be
- able to be evaluated at compile time and must not depend on
- run-time information in order to be evaluated.
+ or <literal>private</literal>, optionally followed by a type declaration,
+ followed by a normal variable declaration. This declaration may
+ include an initialization, but this initialization must be a constant
+ value--that is, it must be able to be evaluated at compile time and
+ must not depend on run-time information in order to be evaluated.
</para>
<para>
See <xref linkend="language.oop5.visibility" /> for more
@@ -131,6 +131,46 @@
Nowdoc and Heredoc support was added in PHP 5.3.0.
</para>
</note>
+
+ <para>
+ As of PHP 7.4.0, property definitions can include a type declaration, with the exception of
+ the <literal>callable</literal> type.
+ <example>
+ <title>Example of typed properties</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+
+class User
+{
+ public int $id;
+ public string $name;
+
+ public function __construct(int $id, string $name)
+ {
+ $this->id = $id;
+ $this->name = $name;
+ }
+}
+
+$user = new User(1234, "php");
+echo "ID: " . $user->id;
+echo "\n";
+echo "Name: " . $user->name;
+
+?>
+]]>
+ </programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+ID: 1234
+Name: php
+]]>
+ </screen>
+ </example>
+ </para>
+
</sect1>
<!-- Keep this comment at the end of the file
Modified: phpdoc/en/trunk/language/oop5/variance.xml
===================================================================
--- phpdoc/en/trunk/language/oop5/variance.xml 2019-12-17 17:49:30 UTC (rev 348561)
+++ phpdoc/en/trunk/language/oop5/variance.xml 2019-12-17 20:24:03 UTC (rev 348562)
@@ -4,9 +4,16 @@
<title>Covariance and Contravariance</title>
<para>
+ Prior to PHP 7.4.0, class methods support mainly invariant parameter types and invariant return types.
+ This means that if a method within a parent class has a parameter type or return
+ type of <varname>T</varname>, any child method with corresponding parameter type
+ or return type <emphasis>must also</emphasis> be type <varname>T</varname>.
+ </para>
+
+ <para>
As of PHP 7.4.0, covariance and contravariance is supported. While these concepts
- may <emphasis>sound</emphasis> confusing, in practice they're rather simple, and extremely useful to
- object-oriented programming.
+ may <emphasis>sound</emphasis> confusing, in practice they're rather simple, and
+ extremely useful to object-oriented programming.
</para>
<sect2 xml:id="language.oop5.variance.covariance">
@@ -59,10 +66,10 @@
</informalexample>
<para>
- You'll notice there aren't any methods which return values in this example. We will
+ Note that there aren't any methods which return values in this example. We will
build upon these classes with a few factories which return a new object of class type
- <varname>Animal</varname>, <varname>Cat</varname>, or <varname>Dog</varname>. This is
- where we can see covariance in action!
+ <varname>Animal</varname>, <varname>Cat</varname>, or <varname>Dog</varname>.
+ Covariance will come into play in the next example.
</para>
<informalexample>
@@ -148,8 +155,7 @@
</informalexample>
<para>
- Typically, cats are picky eaters. They will eat a specific type of food, and that's it.
- Dogs, on the other hand, will eat just about anything. Thus, we're going to override the
+ In order to see the behavior of contravariance, we will override the
<varname>eat</varname> method in the <varname>Dog</varname> class to allow any
<varname>Food</varname> type object. The <varname>Cat</varname> class remains unchanged.
</para>
--
PHP Documentation Commits Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic