如何将 instanceof 运算符与抽象 class 一起使用?
How Use instanceof operator with abstract class?
如何将 instanceof 运算符与扩展抽象 class 的这 3 个子 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.
我想在 index.php 中将此代码与 instanceof 运算符一起使用(而不是代码向下),但它无法正常工作。
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;
}
?>
我该如何解决这个问题?
您无法创建抽象实例 class,因为您会遇到致命错误。
但是您可以使用 instanceOf abstractClass 来查看派生的 class 是否扩展了您的抽象 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";
}
如何将 instanceof 运算符与扩展抽象 class 的这 3 个子 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.
我想在 index.php 中将此代码与 instanceof 运算符一起使用(而不是代码向下),但它无法正常工作。
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;
}
?>
我该如何解决这个问题?
您无法创建抽象实例 class,因为您会遇到致命错误。
但是您可以使用 instanceOf abstractClass 来查看派生的 class 是否扩展了您的抽象 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";
}