当前位置: 移动技术网 > IT编程>开发语言>PHP > Laravel中Trait的用法实例详解

Laravel中Trait的用法实例详解

2017年12月12日  | 移动技术网IT编程  | 我要评论
本文实例讲述了laravel中trait的用法。分享给大家供大家参考,具体如下: 看看php官方手册对trait的定义: 自 php 5.4.0 起,php 实现了代码

本文实例讲述了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;
  /* ... */
}
?>

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();
?>

以上例程会输出:

hello world!

以上内容来自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]);
}

这里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了,很好的实现了代码的复用。

个人理解:

在一个类中使用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程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网