PHP(5.4)中是否有任何函数可以将使用的特征作为数组或类似函数:
class myClass extends movingThings {
use bikes, tanks;
__construct() {
echo 'I\'m using the two traits:' . ????; // bikes, tanks
}
}
Run Code Online (Sandbox Code Playgroud) 请注意,特征可能使用其他特征,因此该类可能不会直接使用该特征.并且该类可以从使用该特征的父类继承.
这是一个可以在几行内解决的问题,还是我必须做一些循环?
我有一个关于在PHP中使用特性和接口的问题。
具有foobar功能的特征
<?php
trait FoobarTrait
{
protected $foobar;
public function setFoobar($foobar)
{
$this->foobar = $foobar
}
public function getFoobar()
{
return $this->foobar;
}
}
Run Code Online (Sandbox Code Playgroud)
用于指定如何使用Trait的特定界面
<?php
interface FoobarInterface
{
public function setFoobar($foobar);
public function getFoobar();
}
Run Code Online (Sandbox Code Playgroud)
我想在一个类中使用foobar功能。什么是最好的方法 ?
是否有必要使用接口来实现并指定特征,或者这是诱发行为?
<?php
class FoobarClass implements FoobarInterface
{
use FoobarTrait;
}
Run Code Online (Sandbox Code Playgroud)
或这个
<?php
class FoobarClass
{
use FoobarTrait;
}
Run Code Online (Sandbox Code Playgroud)
感谢您的答复和辩论;)