I have a small problem where I couldn't find the answer on internet and I'm not quite sure how php and interfaces work.
So the problem is I have a if(!variable instanceof class). But here, the checked class is an interface and it is suppose to be in a array as you can see in the following code
abstract class Action
{
final public function call(Bone $bone)
{
$sites = $this->getSites($bone);
foreach ($sites as $site) {
if (!$site instanceof Site) {
throw new \Exception("Invalid entry");
}
}
}
}
class BonesSites
{
public function getSites(string $site): array
{
if ($site === 'Egypt') {
return [
[
'siteId' => 1,
'name' => 'Cairo',
'bone' => 'T-Rex bones',
],
[
'siteId' => 2,
'name' => 'Giza',
'bone' => 'Raptors bones',
],
[
'siteId' => 3,
'name' => 'Alexandria',
'bone' => 'Bronchiosaurus bones',
],
];
}
return ['error' => 'Site not found!'];
}
}
interface Bone
{
public function getName(): string;
}
interface Site
{
}
Any idea how to return an interface within an array?
this array is supposed to return all the site with id, name and bone but also be type of class Site...well then the new class will need to have all those properties. But where does an interface come into that? Your current Site interface is pointless because it doesn't specify any properties or functions that an implementing class must provide