mirror of
https://github.com/php/doc-en.git
synced 2026-03-23 23:32:18 +01:00
Use simpara according to new style guide Fix indentation Use semantic line breaks and reduce line length to 80 chars were reasonable as dictated by our style guide
321 lines
7.8 KiB
XML
321 lines
7.8 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. Contravariance allows a parameter type to be less
|
|
specific in a child method, than that of its parent.
|
|
</para>
|
|
<para>
|
|
A type declaration is considered more specific in the following case:
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
A type is removed from a
|
|
<link linkend="language.types.type-system.composite.union">union type</link>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
A type is added to an
|
|
<link linkend="language.types.type-system.composite.intersection">intersection type</link>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
A class type is changed to a child class type
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<type>iterable</type> is changed to <type>array</type> or <classname>Traversable</classname>
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
|
|
A type class is considered less specific if the opposite is true.
|
|
</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 <methodname>eat</methodname> 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>
|
|
<sect2>
|
|
<title>Property variance</title>
|
|
<simpara>
|
|
By default, properties are neither covariant nor contravariant, hence invariant.
|
|
That is, their type may not change in a child class at all.
|
|
The reason for that is "get" operations must be covariant,
|
|
and "set" operations must be contravariant.
|
|
The only way for a property to satisfy both requirements is to be invariant.
|
|
</simpara>
|
|
<simpara>
|
|
As of PHP 8.4.0, with the addition of abstract properties (on an interface or abstract class) and
|
|
<link linkend="language.oop5.property-hooks.virtual">virtual properties</link>,
|
|
it is possible to declare a property that has only a get or set operation.
|
|
As a result, abstract properties or virtual properties that have only a "get" operation required may be covariant.
|
|
Similarly, an abstract property or virtual property that has only a "set" operation required may be contravariant.
|
|
</simpara>
|
|
<simpara>
|
|
Once a property has both a get and set operation, however,
|
|
it is no longer covariant or contravariant for further extension.
|
|
That is, it is now invariant.
|
|
</simpara>
|
|
<example>
|
|
<title>Property type variance</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
class Animal {}
|
|
class Dog extends Animal {}
|
|
class Poodle extends Dog {}
|
|
|
|
interface PetOwner
|
|
{
|
|
// Only a get operation is required, so this may be covariant.
|
|
public Animal $pet { get; }
|
|
}
|
|
|
|
class DogOwner implements PetOwner
|
|
{
|
|
// This may be a more restrictive type since the "get" side
|
|
// still returns an Animal. However, as a native property
|
|
// children of this class may not change the type anymore.
|
|
public Dog $pet;
|
|
}
|
|
|
|
class PoodleOwner extends DogOwner
|
|
{
|
|
// This is NOT ALLOWED, because DogOwner::$pet has both
|
|
// get and set operations defined and required.
|
|
public Poodle $pet;
|
|
}
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</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
|
|
-->
|