Laravel中Trait的用法实例详解,laraveltrait_PHP教程

WBOY
Release: 2016-07-12 08:56:48
Original
981 people have browsed it

Laravel中Trait的用法实例详解,laraveltrait

本文实例讲述了Laravel中Trait的用法。分享给大家供大家参考,具体如下:

看看PHP官方手册对Trait的定义:

自 PHP 5.4.0 起,PHP 实现了代码复用的一个方法,称为 traits。

Traits 是一种为类似 PHP 的单继承语言而准备的代码复用机制。Trait 为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用方法集。Traits 和类组合的语义是定义了一种方式来减少复杂性,避免传统多继承和混入类(Mixin)相关的典型问题。

Trait 和一个类相似,但仅仅旨在用细粒度和一致的方式来组合功能。Trait 不能通过它自身来实例化。它为传统继承增加了水平特性的组合;也就是说,应用类的成员不需要继承。

官方手册也举了两个例子:

Trait用法示例

<?php
trait ezcReflectionReturnInfo {
  function getReturnType() { /*1*/ }
  function getReturnDescription() { /*2*/ }
}
class ezcReflectionMethod extends ReflectionMethod {
  use ezcReflectionReturnInfo;
  /* ... */
}
class ezcReflectionFunction extends ReflectionFunction {
  use ezcReflectionReturnInfo;
  /* ... */
}
?>

Copy after login

Trait的优先级

从基类继承的成员被 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();
?>

Copy after login

以上例程会输出:

Hello World!

Copy after login

以上内容来自PHP官网手册。

Trait在Laravel中的使用

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]);
}

Copy after login

这里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子
}

Copy after login

其中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);
  }
}

Copy after login

这样就可以在控制中使用Trait了,很好的实现了代码的复用。

个人理解:

在一个类中使用Trait,就相当于这个类也有了Trait中定义的属性和方法。Traits的使用场景是如果多个类都要用到同样的属性或者方法,这个时候使用Traits可以方便的给类增加这些属性或方法,而不用每个类都去继承一个类,如果说继承类是竖向扩展一个类,那么Traits是横向扩展一个类,从而实现代码复用。

关于PHP中Trait的使用还可参考前面一篇《PHP中的traits简单使用实例》

本文转自:小谈博客 http://www.tantengvip.com/2015/12/laravel-trait/

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • PHP的Laravel框架结合MySQL与Redis数据库的使用部署
  • PHP的Laravel框架中使用消息队列queue及异步队列的方法
  • Laravel执行migrate命令提示:No such file or directory的解决方法
  • Laravel中注册Facades的步骤详解
  • Laravel实现构造函数自动依赖注入的方法
  • Laravel使用Caching缓存数据减轻数据库查询压力的方法
  • 基于laravel制作APP接口(API)
  • 详解PHP的Laravel框架中Eloquent对象关系映射使用
  • Laravel框架数据库CURD操作、连贯操作总结
  • 深入解析PHP的Laravel框架中的event事件操作

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1111346.htmlTechArticleLaravel中Trait的用法实例详解,laraveltrait 本文实例讲述了Laravel中Trait的用法。分享给大家供大家参考,具体如下: 看看PHP官方手册对Trait的定...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!