Snippets

Oleg Chulakov Studio Ковариантность и контравариантность

Created by Chulakov Deploy last modified
<?php
class JuniorCatDoctor
{
    public function heal(Cat $cat): void
    {
        // Many code to heal cat
    }
}

class SeniorCatDoctor extends JuniorCatDoctor
{
    public function heal(Animal $cat): void
    {
        // If cat use standart code to heal cat
        // Else analises animal and try to heal
    }
}

$cat = new Cat('Murka');
$dog = new Dog('Sharik');

$juniorDoctor = new JuniorCatDoctor();
$seniorDoctor = new SeniorCatDoctor();

$juniorDoctor->heal($cat);
// $juniorDoctor->heal($dog); // Kernel panic!

$seniorDoctor->heal($cat);
$seniorDoctor->heal($dog); // Dog still live
class JuniorCatDoctor
{
    heal(cat: Cat): void {
        // Many code to heal cat
    }
}

class SeniorCatDoctor extends JuniorCatDoctor
{
    heal(cat: Animal): void {
        // If cat use standart code to heal cat
        // Else analises animal and try to heal
    }
}

const cat = new Cat('Murka');
const dog = new Dog('Sharik');

const juniorDoctor = new JuniorCatDoctor();
const seniorDoctor = new SeniorCatDoctor();

juniorDoctor.heal(cat);
juniorDoctor.heal(dog); // Kernel panic!

seniorDoctor.heal(cat);
seniorDoctor.heal(dog); // Dog still live
<?php
interface AnimalRepository
{
    public function fetchAnimal(): Animal;
}

class CatRepository implements AnimalRepository
{
    public function fetchAnimal(): Cat
    {
        return new Cat('Murka');
    }
}

class DogRepository implements AnimalRepository
{
    public function fetchAnimal(): Dog
    {
        return new Dog('Sharik');
    }
}

$cat = (new CatRepository())->fetchAnimal();
$dog = (new DogRepository())->fetchAnimal();

echo $cat->say(); // My name is Murka and I am is a cat
echo "\n";
echo $dog->say(); // My name is Sharik and I am is a dog
interface AnimalRepository
{
    fetchAnimal(): Animal;
}

class CatRepository implements AnimalRepository
{
    fetchAnimal(): Cat {
        return new Cat('Murka');
    }
}

class DogRepository implements AnimalRepository
{
    fetchAnimal(): Dog {
        return new Dog('Sharik');
    }
}

const cat = (new CatRepository()).fetchAnimal();
const dog = (new DogRepository()).fetchAnimal();

console.log(cat.say()); // My name is Murka and I am is a cat
console.log(dog.say()); // My name is Sharik and I am is a dog
<?php
class Animal
{
    public function __construct(
        private string $name,
    ) {}
    
    public function say(): string
    {
        return "My name is {$this->name}";
    }
}

class Cat extends Animal
{
    public function say(): string
    {
        return parent::say() . " and I am is a cat";
    }
}

class Dog extends Animal
{
    public function say(): string
    {
        return parent::say() . " and I am is a dog";
    }
}
class Animal {
  name: string;
  constructor(name: string) {
    this.name = name;
  }
  say(): string {
    return "My name is " + this.name;
  }
}

class Cat extends Animal {
    say(): string {
        return super.say() + " and I am is a cat";
    }
    meow(): string {
        return "Meow";
    }
}

class Dog extends Animal {
    say(): string {
        return super.say() + " and I am is a dog";
    }
    woof(): string {
        return "Woof";
    }
}

Comments (1)

  1. Linda Melson
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.