<?php
/**
* Interface with type hints in method
*/
interface Foo
{
public function doStuff(string $str, Iterator $iterator);
}
/**
* Abstract class with type hints in method
*/
abstract class Bar
{
public function doStuff(string $str, Iterator $iterator)
{
echo __class__ . PHP_EOL;
}
}
/**
* Class with type hints in method
*/
class Baz
{
public function doStuff(string $str, Iterator $iterator)
{
echo __class__ . PHP_EOL;
}
}
/**
* Class implements interface but omits type hints
*/
$object = new class() implements Foo {
public function doStuff($str, $iterator)
{
echo $str . PHP_EOL;
}
};
$object->doStuff('Interface', new EmptyIterator());
/**
* Class extends abstract class but omits type hints
*/
$object = new class() extends Bar {
/**
* Overwrites method of abstract class
*/
public function doStuff($str, $iterator)
{
echo $str . PHP_EOL;
}
};
$object->doStuff('Abstract', new EmptyIterator());
/**
* Class extends other regular class but omits type hints
*/
$object = new class() extends Baz {
/**
* Overwrites method of class, but keeps first type hint
*/
public function doStuff(string $str, $iterator)
{
echo $str . PHP_EOL;
}
};
$object->doStuff('Class', new EmptyIterator());