mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 00:48:54 +00:00
Formalize wording in variance page, remove any wording that speaks to the audience
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@349390 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
55640d87ac
commit
62be58faa7
1 changed files with 24 additions and 31 deletions
|
@ -4,29 +4,23 @@
|
|||
<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>.
|
||||
In PHP 7.2.0, partial contravariance was introduced by removing type restrictions
|
||||
on parameters in a child method. As of PHP 7.4.0, full covariance and contravariance
|
||||
support was added.
|
||||
</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.
|
||||
Covariance allows a child's method to return a more specific type than the return type
|
||||
of its parent's method. Whereas, contravariance allows a parameter type to be less
|
||||
specific in a child method, than that of its parent.
|
||||
</para>
|
||||
|
||||
<sect2 xml:id="language.oop5.variance.covariance">
|
||||
<title>Covariance</title>
|
||||
|
||||
<para>
|
||||
Covariance allows a child's method to return a more specific type than the return
|
||||
type of its parent's method. This is better illustrated with an example.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
We'll start with a simple abstract parent class, <varname>Animal</varname>, which is
|
||||
extended by children classes, <varname>Cat</varname>, and <varname>Dog</varname>.
|
||||
To illustrate how covariance works, a simple abstract parent class, <varname>Animal</varname>
|
||||
is created. <varname>Animal</varname> will be extended by children classes,
|
||||
<varname>Cat</varname>, and <varname>Dog</varname>.
|
||||
</para>
|
||||
|
||||
<informalexample>
|
||||
|
@ -66,10 +60,9 @@ class Cat extends Animal
|
|||
</informalexample>
|
||||
|
||||
<para>
|
||||
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>.
|
||||
Covariance will come into play in the next example.
|
||||
Note that there aren't any methods which return values in this example. A few factories
|
||||
will be added which return a new object of class type <varname>Animal</varname>,
|
||||
<varname>Cat</varname>, or <varname>Dog</varname>.
|
||||
</para>
|
||||
|
||||
<informalexample>
|
||||
|
@ -120,11 +113,11 @@ Mavrick barks
|
|||
<title>Contravariance</title>
|
||||
|
||||
<para>
|
||||
Contravariance, on the other hand, allows a parameter type to be less specific in a child
|
||||
method, than that of its parent. Continuing with our previous example with the classes
|
||||
<varname>Animal</varname>, <varname>Cat</varname>, and <varname>Dog</varname>, we're adding
|
||||
a class called <varname>Food</varname> and <varname>AnimalFood</varname>, and adding a method
|
||||
<varname>eat(AnimalFood $food)</varname> to our <varname>Animal</varname> abstract class.
|
||||
Continuing with the previous example with the classes <varname>Animal</varname>,
|
||||
<varname>Cat</varname>, and <varname>Dog</varname>, a class called
|
||||
<varname>Food</varname> and <varname>AnimalFood</varname> will be included, and
|
||||
a method <varname>eat(AnimalFood $food)</varname> is added to the <varname>Animal</varname>
|
||||
abstract class.
|
||||
</para>
|
||||
|
||||
<informalexample>
|
||||
|
@ -147,7 +140,7 @@ abstract class Animal
|
|||
|
||||
public function eat(AnimalFood $food)
|
||||
{
|
||||
echo $this->name . " nom noms " . get_class($food);
|
||||
echo $this->name . " eats " . get_class($food);
|
||||
}
|
||||
}
|
||||
]]>
|
||||
|
@ -155,9 +148,9 @@ abstract class Animal
|
|||
</informalexample>
|
||||
|
||||
<para>
|
||||
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.
|
||||
In order to see the behavior of contravariance, the
|
||||
<varname>eat</varname> method is overridden in the <varname>Dog</varname> class to allow
|
||||
any <varname>Food</varname> type object. The <varname>Cat</varname> class remains unchanged.
|
||||
</para>
|
||||
|
||||
<informalexample>
|
||||
|
@ -176,7 +169,7 @@ class Dog extends Animal
|
|||
</informalexample>
|
||||
|
||||
<para>
|
||||
Now, we can see how contravariance works.
|
||||
The next example will show the behavior of contravariance.
|
||||
</para>
|
||||
|
||||
<informalexample>
|
||||
|
@ -197,8 +190,8 @@ $doggy->eat($banana);
|
|||
&example.outputs;
|
||||
<screen>
|
||||
<![CDATA[
|
||||
Ricky nom noms AnimalFood
|
||||
Mavrick nom noms Food
|
||||
Ricky eats AnimalFood
|
||||
Mavrick eats Food
|
||||
]]>
|
||||
</screen>
|
||||
|
||||
|
|
Loading…
Reference in a new issue