I got E_COMPILE_ERROR when i'm trying to run the following code:
<?php
interface DataInterface
{
public function get();
}
interface ServiceInterface
{
public function save(DataInterface $data);
}
class Data implements DataInterface
{
public function get()
{
return 'data';
}
}
class Service implements ServiceInterface
{
public function save(Data $data)
{//the problem is here^^
var_dump($data->get());
}
}
$service = new Service();
$data = new Data();
$service->save($data);
Data class is implementation of DataInterface interface. I wonder why this code cannot be compiled? Documentation says that valid type must be an instanceof the given class or interface name.
(http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration).
$data = new Data();
var_dump($data instanceof DataInterface); //true;
As far as i understand if declared type of method parameter is class which implements expected interface then this type satisfies the needs (implements all methods) and signature should match.