- rand: documentation ( source)
<?php
// Invalid example
class Animal {
public function eat(){}
}
class Cat extends Animal {
public function meow(){
echo "meow";
}
}
class A {
function foo(): Animal {
return new Animal();
}
}
class B extends A {
function foo(): Cat {
return new Cat();
}
}
if (rand(0, 1)) {
$object = new A;
}
else {
$object = new B;
}
$animal = $object->foo();
$animal->meow();
// This code fails %50 of the time as not all animals can meow.