[英]Multiple Extends in PHP
我聽說您不能兩次使用擴展。
我有兩節課:
Base32和SecureRandom
我需要TOTP。
我怎樣才能同時使用它們呢?
看一下PHP5.4特性,他們解決了多種擴展問題。
將它們與接口結合起來以獲得instanceof功能。
例如:
interface ClientAwareInterface {
public function setClient($client);
}
trait ClientAwareTrait {
protected $client;
public function setClient($client)
{
$this->client = $client;
}
}
class Shop implements ClientAwareInterface extends SomeClass {
use ClientAwareTrait; // use our trait to implement interface methods
use OtherTrait;
}
$shop = new Shop();
if($shop instanceof ClientAwareInterface) {
$shop->setClient('test');
var_dump($shop);
}
結果將是:
object(Shop)[1]
protected 'client' => string 'test' (length=4)
PHP不允許多重繼承。 您需要擴展它們中的一個,並將另一個作為私有變量或類似的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.