From 62be58faa7f0bf4ce6b0fa62b393fcad047e4d9a Mon Sep 17 00:00:00 2001 From: Tiffany Taylor Date: Thu, 12 Mar 2020 14:36:14 +0000 Subject: [PATCH] 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 --- language/oop5/variance.xml | 55 +++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/language/oop5/variance.xml b/language/oop5/variance.xml index 2f116f1054..d2ab69407c 100644 --- a/language/oop5/variance.xml +++ b/language/oop5/variance.xml @@ -4,29 +4,23 @@ Covariance and Contravariance - 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 T, any child method with corresponding parameter type - or return type must also be type T. + 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. - - As of PHP 7.4.0, covariance and contravariance is supported. While these concepts - may sound 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. Covariance - 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. - - - - We'll start with a simple abstract parent class, Animal, which is - extended by children classes, Cat, and Dog. + To illustrate how covariance works, a simple abstract parent class, Animal + is created. Animal will be extended by children classes, + Cat, and Dog. @@ -66,10 +60,9 @@ class Cat extends Animal - 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 - Animal, Cat, or Dog. - 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 Animal, + Cat, or Dog. @@ -120,11 +113,11 @@ Mavrick barks Contravariance - 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 - Animal, Cat, and Dog, we're adding - a class called Food and AnimalFood, and adding a method - eat(AnimalFood $food) to our Animal abstract class. + Continuing with the previous example with the classes Animal, + Cat, and Dog, a class called + Food and AnimalFood will be included, and + a method eat(AnimalFood $food) is added to the Animal + abstract class. @@ -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 - In order to see the behavior of contravariance, we will override the - eat method in the Dog class to allow any - Food type object. The Cat class remains unchanged. + In order to see the behavior of contravariance, the + eat method is overridden in the Dog class to allow + any Food type object. The Cat class remains unchanged. @@ -176,7 +169,7 @@ class Dog extends Animal - Now, we can see how contravariance works. + The next example will show the behavior of contravariance. @@ -197,8 +190,8 @@ $doggy->eat($banana); &example.outputs;