Search code examples
phpoopinterfaceabstract-classinstanceof

How Use instanceof operator with abstract class?


How Can I use instanceof operator with these 3 child class that extends abstract class?

When I instantiate all them it outputs result with only one product - Book Specific data. With Other 2 products (Disc, Furniture) doesn't display specific data and doesn't add data to SQL Data Base too.

I want use this code with instanceof operator in index.php (instead of code down), But it doest't work correctly.

function show($user) {
    if($user instanceof HavingWeight)
    {
      $user->setWeight($weight);
    } elseif ($user instanceof HavingSize)
    {
      $user->setSize($size);
    } elseif($user instanceof HavingFur_dims)
    {
      $user->setHeight($height);
      $user->setWidth($width);
      $user->setLength($length);
    } else 
    { 
      die("This is not a Product..");
    }
}

show(new Book);
show(new Disc);
show(new Furniture);

index.php

$user = new Book();
$Size = new Disc();
$Fur = new Furniture();

$user->setWeight($weight);    
$Size->setSize($size);    
$Fur->setHeight($height);    
$Fur->setWidth($width);    
$Fur->setLength($length); 

Product.php

abstract class Product
{
  // All common properties and methods
}

Book.php

<?php 

// interfaces
interface HavingWeight
{
    public function setWeight($weight);
}

// traits
trait WithWeight
{
    // setters
    public function setWeight($weight)
    {
        $this->weight = $weight;
    }
}

// Child classes
class Book extends Product implements HavingWeight
{
   use WithWeight;
}

?>

Furniture.php

<?php 
include_once 'classes/Product.php'; 

// interfaces of each product type
interface HavingFur_dims
{
    public function setHeight($height);
    public function setWidth($width);
    public function setLength($length);
}

// traits of each product type
trait WithFur_dims
{
    // setters
    public function setHeight($height)
    {
        return $this->height = $height;
    }

    public function setWidth($width)
    {
        return $this->width = $width;
    }

    public function setLength($length)
    {
        return $this->length = $length;
    }
}

// Child classes
class Furniture extends Product implements HavingFur_dims
{
   use WithFur_dims;
}

?>

Disc.php

<?php 

// interfaces of each product type
interface HavingSize
{
    public function setSize($size);
}

// traits of each product type
trait WithSize
{
    // setters    
    public function setSize($size)
    {
        $this->size = $size;
    }
}

// Child classes
class Disc extends Product implements HavingSize
{
   use WithSize;
}

?>

How can I fix this problem?


Solution

  • You can not create an instance of an abstract class, because you get a fatal error.

    But you can use instanceOf abstractClass to see if the derived class extends your abstract class.

    abstract class Product
    {
      // All common properties and methods
    }
    
    class ProductEx1 extends Product{}
    
    
    $inst = new ProductEx1;
    if($inst instanceOf Product){
      echo "Yes, ProductEx1 is also a instance of Product";
    }