Search code examples
phparraysclassinterface

PHP - Return an instance of interface into an array


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?


Solution

  • 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'))
                ];