源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Laravel中Trait的用法实例详解

  • 时间:2020-10-01 17:35 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Laravel中Trait的用法实例详解
本文实例讲述了Laravel中Trait的用法。分享给大家供大家参考,具体如下: 看看PHP官方手册对Trait的定义: 自 PHP 5.4.0 起,PHP 实现了代码复用的一个方法,称为 traits。 Traits 是一种为类似 PHP 的单继承语言而准备的代码复用机制。Trait 为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用方法集。Traits 和类组合的语义是定义了一种方式来减少复杂性,避免传统多继承和混入类(Mixin)相关的典型问题。 Trait 和一个类相似,但仅仅旨在用细粒度和一致的方式来组合功能。Trait 不能通过它自身来实例化。它为传统继承增加了水平特性的组合;也就是说,应用类的成员不需要继承。 官方手册也举了两个例子: [b]Trait用法示例[/b]
<?php
trait ezcReflectionReturnInfo {
  function getReturnType() { /*1*/ }
  function getReturnDescription() { /*2*/ }
}
class ezcReflectionMethod extends ReflectionMethod {
  use ezcReflectionReturnInfo;
  /* ... */
}
class ezcReflectionFunction extends ReflectionFunction {
  use ezcReflectionReturnInfo;
  /* ... */
}
?>

[b]Trait的优先级[/b] 从基类继承的成员被 trait 插入的成员所覆盖。优先顺序是来自当前类的成员覆盖了 trait 的方法,而 trait 则覆盖了被继承的方法。 从基类继承的成员被插入的 SayWorld Trait 中的 MyHelloWorld 方法所覆盖。其行为 MyHelloWorld 类中定义的方法一致。优先顺序是当前类中的方法会覆盖 trait 方法,而 trait 方法又覆盖了基类中的方法。
<?php
class Base {
  public function sayHello() {
    echo 'Hello ';
  }
}
trait SayWorld {
  public function sayHello() {
    parent::sayHello();
    echo 'World!';
  }
}
class MyHelloWorld extends Base {
  use SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
?>

以上例程会输出:
Hello World!

以上内容来自PHP官网手册。 [b]Trait在Laravel中的使用[/b] Laravel中大量使用Trait特性来提高代码的复用性,本文只是从某个Laravel项目中举个例子。 比如在一个PageController.php控制器中有个show方法:
public function show($slug)
{
  $page = PageRepository::find($slug);
  $this->checkPage($page, $slug);
 
  return View::make('pages.show', ['page' => $page]);
}

这里PageRepository::find()方法就是使用的一个Trait的方法,在PageRepository.php中使用命名空间声明及引入:
namespace GrahamCampbell\BootstrapCMS\Repositories;
use GrahamCampbell\Credentials\Repositories\AbstractRepository;
use GrahamCampbell\Credentials\Repositories\PaginateRepositoryTrait;
use GrahamCampbell\Credentials\Repositories\SlugRepositoryTrait;
class PageRepository extends AbstractRepository
{
  use PaginateRepositoryTrait, SlugRepositoryTrait;
  // 此处省略800子
}

其中SlugRepositoryTrait这个Trait定义了find方法:
trait SlugRepositoryTrait
{
  /**
   * Find an existing model by slug.
   *
   * @param string  $slug
   * @param string[] $columns
   *
   * @return \Illuminate\Database\Eloquent\Model
   */
  public function find($slug, array $columns = ['*'])
  {
    $model = $this->model;
    return $model::where('slug', '=', $slug)->first($columns);
  }
}

这样就可以在控制中使用Trait了,很好的实现了代码的复用。 [b]个人理解:[/b] 在一个类中使用Trait,就相当于这个类也有了Trait中定义的属性和方法。Traits的使用场景是如果多个类都要用到同样的属性或者方法,这个时候使用Traits可以方便的给类增加这些属性或方法,而不用每个类都去继承一个类,如果说继承类是竖向扩展一个类,那么Traits是横向扩展一个类,从而实现代码复用。 关于PHP中Trait的使用还可参考前面一篇《[url=http://www.1sucai.cn/article/66017.htm]PHP中的traits简单使用实例[/url]》 本文转自:小谈博客 http://www.tantengvip.com/2015/12/laravel-trait/ 更多关于Laravel相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/583.htm]Laravel框架入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/155.htm]php优秀开发框架总结[/url]》、《[url=http://www.1sucai.cn/Special/26.htm]smarty模板入门基础教程[/url]》、《[url=http://www.1sucai.cn/Special/96.htm]php日期与时间用法总结[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部