mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-18 18:08:54 +00:00

(thanks salathe) git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@349392 c90b9560-bf6c-de11-be94-00142212c4b1
236 lines
5.2 KiB
XML
236 lines
5.2 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<sect1 xml:id="language.oop5.variance" xmlns="http://docbook.org/ns/docbook">
|
|
<title>Covariance and Contravariance</title>
|
|
|
|
<para>
|
|
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>
|
|
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>
|
|
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>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
abstract class Animal
|
|
{
|
|
protected string $name;
|
|
|
|
public function __construct(string $name)
|
|
{
|
|
$this->name = $name;
|
|
}
|
|
|
|
abstract public function speak();
|
|
}
|
|
|
|
class Dog extends Animal
|
|
{
|
|
public function speak()
|
|
{
|
|
echo $this->name . " barks";
|
|
}
|
|
}
|
|
|
|
class Cat extends Animal
|
|
{
|
|
public function speak()
|
|
{
|
|
echo $this->name . " meows";
|
|
}
|
|
}
|
|
]]>
|
|
</programlisting>
|
|
</informalexample>
|
|
|
|
<para>
|
|
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>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
interface AnimalShelter
|
|
{
|
|
public function adopt(string $name): Animal;
|
|
}
|
|
|
|
class CatShelter implements AnimalShelter
|
|
{
|
|
public function adopt(string $name): Cat // instead of returning class type Animal, it can return class type Cat
|
|
{
|
|
return new Cat($name);
|
|
}
|
|
}
|
|
|
|
class DogShelter implements AnimalShelter
|
|
{
|
|
public function adopt(string $name): Dog // instead of returning class type Animal, it can return class type Dog
|
|
{
|
|
return new Dog($name);
|
|
}
|
|
}
|
|
|
|
$kitty = (new CatShelter)->adopt("Ricky");
|
|
$kitty->speak();
|
|
echo "\n";
|
|
|
|
$doggy = (new DogShelter)->adopt("Mavrick");
|
|
$doggy->speak();
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
Ricky meows
|
|
Mavrick barks
|
|
]]>
|
|
</screen>
|
|
</informalexample>
|
|
</sect2>
|
|
|
|
<sect2 xml:id="language.oop5.variance.contravariance">
|
|
<title>Contravariance</title>
|
|
|
|
<para>
|
|
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>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
class Food {}
|
|
|
|
class AnimalFood extends Food {}
|
|
|
|
abstract class Animal
|
|
{
|
|
protected string $name;
|
|
|
|
public function __construct(string $name)
|
|
{
|
|
$this->name = $name;
|
|
}
|
|
|
|
public function eat(AnimalFood $food)
|
|
{
|
|
echo $this->name . " eats " . get_class($food);
|
|
}
|
|
}
|
|
]]>
|
|
</programlisting>
|
|
</informalexample>
|
|
|
|
<para>
|
|
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>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
class Dog extends Animal
|
|
{
|
|
public function eat(Food $food) {
|
|
echo $this->name . " eats " . get_class($food);
|
|
}
|
|
}
|
|
]]>
|
|
</programlisting>
|
|
</informalexample>
|
|
|
|
<para>
|
|
The next example will show the behavior of contravariance.
|
|
</para>
|
|
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
$kitty = (new CatShelter)->adopt("Ricky");
|
|
$catFood = new AnimalFood();
|
|
$kitty->eat($catFood);
|
|
echo "\n";
|
|
|
|
$doggy = (new DogShelter)->adopt("Mavrick");
|
|
$banana = new Food();
|
|
$doggy->eat($banana);
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
Ricky eats AnimalFood
|
|
Mavrick eats Food
|
|
]]>
|
|
</screen>
|
|
|
|
<para>
|
|
But what happens if <varname>$kitty</varname> tries to <varname>eat</varname> the
|
|
<varname>$banana</varname>?
|
|
</para>
|
|
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
$kitty->eat($banana);
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
Fatal error: Uncaught TypeError: Argument 1 passed to Animal::eat() must be an instance of AnimalFood, instance of Food given
|
|
]]>
|
|
</screen>
|
|
</informalexample>
|
|
</sect2>
|
|
</sect1>
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|