october / extension
此包已 废弃 且不再维护。没有建议的替代包。
基于扩展的类,例如私有特质
v1.0.419
2017-04-28 23:25 UTC
Requires
- php: >=5.5.9
- dev-master / 1.0.x-dev
- 1.0.x-dev
- v1.0.419
- v1.0.418
- v1.0.417
- v1.0.416
- v1.0.415
- v1.0.414
- v1.0.413
- v1.0.412
- v1.0.411
- v1.0.410
- v1.0.409
- v1.0.408
- v1.0.407
- v1.0.406
- v1.0.405
- v1.0.404
- v1.0.403
- v1.0.402
- v1.0.401
- v1.0.400
- v1.0.399
- v1.0.398
- v1.0.397
- v1.0.396
- v1.0.395
- v1.0.394
- v1.0.393
- v1.0.392
- v1.0.391
- v1.0.390
- v1.0.389
- v1.0.388
- v1.0.387
- v1.0.386
- v1.0.385
- v1.0.384
- v1.0.383
- v1.0.382
- v1.0.381
- v1.0.380
- v1.0.379
- v1.0.378
- v1.0.377
- v1.0.376
- v1.0.375
- v1.0.374
- v1.0.373
- v1.0.372
- v1.0.371
- v1.0.370
- v1.0.369
- v1.0.368
- v1.0.367
- v1.0.366
- v1.0.365
- v1.0.364
- v1.0.363
- v1.0.362
- v1.0.361
- v1.0.360
- v1.0.359
- v1.0.358
- v1.0.357
- v1.0.356
- v1.0.355
- v1.0.354
- v1.0.353
- v1.0.352
- v1.0.351
- v1.0.350
- v1.0.349
- v1.0.348
- v1.0.347
- v1.0.346
- v1.0.345
- v1.0.344
- v1.0.343
- v1.0.342
- v1.0.341
- v1.0.340
- v1.0.339
- v1.0.338
- v1.0.337
- v1.0.336
- v1.0.335
- v1.0.334
- v1.0.333
- v1.0.332
- v1.0.331
- v1.0.330
- v1.0.329
- v1.0.328
- v1.0.327
- v1.0.326
- v1.0.325
- v1.0.324
- v1.0.323
- v1.0.322
- v1.0.321
- v1.0.320
- v1.0.319
- dev-develop
- dev-l55upgrade
- dev-stable
- dev-halcyon
- dev-laravel5
This package is not auto-updated.
Last update: 2018-12-08 08:18:01 UTC
README
此扩展为类提供了具有 私有特质(也称为行为)的能力,类似于原生PHP特质,但具有一些独特的优势
- 行为有自己的构造函数。
- 行为可以有私有或受保护的函数。
- 方法和属性名可以安全地冲突。
- 类可以动态地扩展行为。
您可能像这样使用特质
class MyClass
{
use \October\Rain\UtilityFunctions;
use \October\Rain\DeferredBinding;
}
行为的使用方式类似
class MyClass extends \October\Rain\Extension\Extendable
{
public $implement = [
'October.Rain.UtilityFunctions',
'October.Rain.DeferredBinding',
];
}
您可能像这样定义特质
trait UtilityFunctions
{
public function sayHello()
{
echo "Hello from " . get_class($this);
}
}
行为是这样定义的
class UtilityFunctions extends \October\Rain\Extension\ExtensionBase
{
protected $parent;
public function __construct($parent)
{
$this->parent = $parent;
}
public function sayHello()
{
echo "Hello from " . get_class($this->parent);
}
}
扩展的对象总是作为第一个参数传递给行为的构造函数。
使用示例
行为/扩展类
<?php namespace MyNamespace\Behaviors;
class FormController extends \October\Rain\Extension\ExtensionBase
{
/**
* @var Reference to the extended object.
*/
protected $controller;
/**
* Constructor
*/
public function __construct($controller)
{
$this->controller = $controller;
}
public function someMethod()
{
return "I come from the FormController Behavior!";
}
public function otherMethod()
{
return "You might not see me...";
}
}
扩展类
此 Controller
类将实现 FormController
行为,然后这些方法将可用(混合)到类中。我们将重写 otherMethod
方法。
<?php namespace MyNamespace;
class Controller extends \October\Rain\Extension\Extendable
{
/**
* Implement the FormController behavior
*/
public $implement = [
'MyNamespace.Behaviors.FormController'
];
public function otherMethod()
{
return "I come from the main Controller!";
}
}
使用扩展
$controller = new MyNamespace\Controller;
// Prints: I come from the FormController Behavior!
echo $controller->someMethod();
// Prints: I come from the main Controller!
echo $controller->otherMethod();
// Prints: You might not see me...
echo $controller->asExtension('FormController')->otherMethod();
动态使用行为/构造函数扩展
任何使用 Extendable
或 ExtendableTrait
的类都可以通过静态 extend()
方法扩展其构造函数。参数应传递一个闭包,该闭包将在类构造函数的一部分中调用。例如
/**
* Extend the Pizza Shop to include the Master Splinter behavior too
*/
MyNamespace\Controller::extend(function($controller){
// Implement the list controller behavior dynamically
$controller->implement[] = 'MyNamespace.Behaviors.ListController';
});
动态创建方法
可以通过使用 addDynamicMethod
向 Model
添加方法。
Post::extend(function($model) {
$model->addDynamicMethod('getTagsAttribute', function() use ($model) {
return $model->tags()->lists('name');
});
});
软定义
如果行为类不存在,就像特质一样,会抛出 类未找到 错误。在某些情况下,您可能希望抑制此错误,以便在系统中存在模块时进行条件实现。您可以通过在类名开头放置一个 @
符号来执行此操作。
class User extends \October\Rain\Extension\Extendable
{
public $implement = ['@RainLab.Translate.Behaviors.TranslatableModel'];
}
如果类名 RainLab\Translate\Behaviors\TranslatableModel
不存在,则不会抛出错误。这相当于以下代码
class User extends \October\Rain\Extension\Extendable
{
public $implement = [];
public function __construct()
{
if (class_exists('RainLab\Translate\Behaviors\TranslatableModel')) {
$controller->implement[] = 'RainLab.Translate.Behaviors.TranslatableModel';
}
parent::__construct();
}
}
使用特质而不是基类
在某些情况下,您可能不想扩展 ExtensionBase
或 Extendable
类,因为其他需求。因此,您可以使用特质,尽管显然行为方法将不可用于父类。
-
使用
ExtensionTrait
时,应将ExtensionBase
的方法应用于类。 -
使用
ExtendableTrait
时,应将Extendable
的方法应用于类。