1

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?

4
  • What exactly are you expecting to happen here? Your getSites function simply returns an array containing some associative arrays...they are not instances of any class and therefore cannot implement any interface. Commented Aug 12, 2022 at 12:19
  • Anyway you can't "return an interface" from anything. Whatever you return from a function must be a concrete thing (which an interface isn't). You could return an instance of a class which implements a specific interface, for example - or an array of such instances, or whatever else. Commented Aug 12, 2022 at 12:21
  • So I would have to make a new class which implement that interface? What about the type of the interface (I guess now the new class I will make) is in an array, this array is supposed to return all the site with id, name and bone but also be type of class Site Commented Aug 12, 2022 at 12:35
  • 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 Commented Aug 12, 2022 at 12:49

1 Answer 1

2

You would need to create an additional class, named Site, and return an array of objects.

class Site
{
    private int $siteId;
    private string $name;
    private string $bone;

    /**
     * @param int    $siteId
     * @param string $name
     * @param string $bone
     */
    public function __construct(int $siteId, string $name, string $bone)
    {
        $this->siteId = $siteId;
        $this->name = $name;
        $this->bone = $bone;
    }

    /**
     * @return int
     */
    public function getSiteId(): int
    {
        return $this->siteId;
    }

    /**
     * @param int $siteId
     */
    public function setSiteId(int $siteId): void
    {
        $this->siteId = $siteId;
    }

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName(string $name): void
    {
        $this->name = $name;
    }

    /**
     * @return string
     */
    public function getBone(): string
    {
        return $this->bone;
    }

    /**
     * @param string $bone
     */
    public function setBone(string $bone): void
    {
        $this->bone = $bone;
    }

}

And to return the array of sites:

        return
            [
                (new Site(1, 'Cairo', 'T-Rex bones')),
                (new Site(2, 'Giza', 'Raptors bones')),
                (new Site(3, 'Alexandria', 'Bronchiosaurus bones'))
            ];
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Michel, it works, I understand now, I just needed to make a class with the right properties like you did and implement the interface to this class then it worked like a charm :)
Shinjuo, glad I could help! Don't forget to mark the answer as accepted if it has helped you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.