php 抽象 类 和接口
php abstract classes and interfaces
我有以下场景:
abstract class Contractor {
// Generic contractor methods...
}
abstract class PrivatePerson extends Contractor {
// Adds methods specific to private contractors
}
abstract class Company extends Contractor {
// Adds methods specific to Company contractors
}
class CustomerPrivate extends PrivatePerson {
// Customers that are contractors, but physical persons
}
class CustomerCompany extends Company {
// Customers that are contractors, but companies
}
供应商和经销商也是如此,他们可以是私人,也可以是公司。现在的问题如下:如何强制 class CustomerPrivate 和 CustomerCompany 的对象同时成为 class Customer(我还没有定义)的对象,供应商和经销商也一样.在这种情况下使用接口是一种好习惯吗?
interface Customer {
}
class PrivateCustomer extends PrivatePerson implements Customer {
// Customers that are physical persons, but are CUSTOMERS!
}
感谢任何建议!
方法一
class CustomerPrivate extends PrivatePerson
{
public function __construct()
{
if( $this instanceof Customer )
{
//CODE
}
else
{
throw new \Exception("YOU ERROR MESSAGE", 1);
# code...
}
}
}
方法二
$obj = new CustomerPrivate();
if ($obj instanceof Customer)
{
//CODE
}
else
{
# code...
}
您可以对任何 class 执行此操作
已编辑
是的,你可以像你发布的那样连接
interface Customer
{
}
class PrivateCustomer extends PrivatePerson implements Customer
{
// Customers that are physical persons, but are CUSTOMERS!
}
或
你可以使用特质。
特征非常灵活,但仅在 PHP 5.4 或更高版本
中受支持
trait Customer
{
}
class PrivateCustomer extends PrivatePerson
{
use Customer; //trait customer
use OtherTrait;
// Customers that are physical persons, but are CUSTOMERS!
}
编辑 2
根据您的情况,有不同的算法可以解决您的问题。我无法想象整个场景,但从你的问题来看。你想让 Customer 类型在两个不同的树( Person 和 Company )中通用,在这种情况下线性层次结构是一个问题,所以我可能使用了这样的东西。
abstract class Contractor
{
public function isCustomer()
{
return FALSE;
}
}
trait Customer
{
public function isCustomer()
{
return TRUE;
}
}
class CustomerCompany extends Company
{
\use Customer;
public function __construct()
{
if( !$this->isCustomer() )
{
throw new \Exception('ERROR', 1);
}
}
}
好的。我终于明白了!
Trait CustomerTrait {
}
interface Customer {
}
class CustomerCompany extends Company implements Customer {
use CustomerTrait;
}
class CustomerPrivate extends ContractorPrivate implements Customer {
use CustomerTrait;
}
我有以下场景:
abstract class Contractor {
// Generic contractor methods...
}
abstract class PrivatePerson extends Contractor {
// Adds methods specific to private contractors
}
abstract class Company extends Contractor {
// Adds methods specific to Company contractors
}
class CustomerPrivate extends PrivatePerson {
// Customers that are contractors, but physical persons
}
class CustomerCompany extends Company {
// Customers that are contractors, but companies
}
供应商和经销商也是如此,他们可以是私人,也可以是公司。现在的问题如下:如何强制 class CustomerPrivate 和 CustomerCompany 的对象同时成为 class Customer(我还没有定义)的对象,供应商和经销商也一样.在这种情况下使用接口是一种好习惯吗?
interface Customer {
}
class PrivateCustomer extends PrivatePerson implements Customer {
// Customers that are physical persons, but are CUSTOMERS!
}
感谢任何建议!
方法一
class CustomerPrivate extends PrivatePerson
{
public function __construct()
{
if( $this instanceof Customer )
{
//CODE
}
else
{
throw new \Exception("YOU ERROR MESSAGE", 1);
# code...
}
}
}
方法二
$obj = new CustomerPrivate();
if ($obj instanceof Customer)
{
//CODE
}
else
{
# code...
}
您可以对任何 class 执行此操作
已编辑
是的,你可以像你发布的那样连接
interface Customer
{
}
class PrivateCustomer extends PrivatePerson implements Customer
{
// Customers that are physical persons, but are CUSTOMERS!
}
或
你可以使用特质。 特征非常灵活,但仅在 PHP 5.4 或更高版本
中受支持trait Customer
{
}
class PrivateCustomer extends PrivatePerson
{
use Customer; //trait customer
use OtherTrait;
// Customers that are physical persons, but are CUSTOMERS!
}
编辑 2
根据您的情况,有不同的算法可以解决您的问题。我无法想象整个场景,但从你的问题来看。你想让 Customer 类型在两个不同的树( Person 和 Company )中通用,在这种情况下线性层次结构是一个问题,所以我可能使用了这样的东西。
abstract class Contractor
{
public function isCustomer()
{
return FALSE;
}
}
trait Customer
{
public function isCustomer()
{
return TRUE;
}
}
class CustomerCompany extends Company
{
\use Customer;
public function __construct()
{
if( !$this->isCustomer() )
{
throw new \Exception('ERROR', 1);
}
}
}
好的。我终于明白了!
Trait CustomerTrait {
}
interface Customer {
}
class CustomerCompany extends Company implements Customer {
use CustomerTrait;
}
class CustomerPrivate extends ContractorPrivate implements Customer {
use CustomerTrait;
}