1

yii 中的行为签名是什么?这应该是一个函数还是一个类方法?任何人都可以提供示例代码吗?

4

1 回答 1

1

行为没有签名,因为行为旨在为类添加一些功能。唯一的要求是从CBehavior. 就像在 wikidocs中一样:

class SomeClass extends CBehavior
{
    public function add($x, $y) { return $x + $y; }
}

class Test extends CComponent
{
    public $blah;
}

$test = new Test(); 
// Attach behavior
$test->attachbehavior('blah', new SomeClass);

// Now you can call `add` on class Test
$test->add(2, 5);

但是,从 PHP 5.4 开始,您可以使用原生php 实现并具有更多功能的Traits,例如上面带有特征的示例:

// NOTE: No need to extend from any base class
trait SomeClass
{
    public function add($x, $y) { return $x + $y; }
}

class Test extends CComponent
{
    // Will additionally use methods and properties from SomeClass
    use SomeClass;
    public $blah;
}

$test = new Test(); 
// You can call `add` on class Test because of `use SomeClass;`
$test->add(2, 5);

还有更多具有特征的功能

于 2013-08-27T07:59:21.890 回答