_php instances of trait usages in Laravel

Source: Internet
Author: User
Tags php website traits smarty template
The examples in this paper describe the use of trait in laravel. Share to everyone for your reference, as follows:

Check out the PHP official manual for Trait definition:

Since PHP 5.4.0, PHP has implemented a method of code reuse, called traits.

Traits is a code reuse mechanism that is prepared for PHP-like single-inheritance languages. Trait to reduce the limitations of single-inheritance languages, developers are free to reuse the set of methods in separate classes within different hierarchies. The semantics of Traits and class combinations are defined as a way to reduce complexity and avoid typical problems associated with traditional multi-inheritance and mixed-Class (Mixin).

Trait is similar to a class, but only designed to combine functionality in a fine-grained and consistent way. Trait cannot be instantiated by itself. It adds a combination of horizontal attributes to traditional inheritance, meaning that members of the application class do not need inheritance.

The Official handbook also cites two examples:

Trait usage Examples

<?phptrait Ezcreflectionreturninfo {  function Getreturntype () {/*1*/}  function Getreturndescription () {/* 2*/}}class Ezcreflectionmethod extends Reflectionmethod {use  ezcreflectionreturninfo;  /* */}class Ezcreflectionfunction extends Reflectionfunction {use  ezcreflectionreturninfo;  /* ... */}?>

Priority of trait

A member inherited from a base class is overwritten by a member inserted by trait. Precedence is the method from which the member of the current class overrides the trait, and trait overrides the inherited method.

Members inherited from the base class are overwritten by the Myhelloworld method in the inserted Sayworld Trait. Its behavior is consistent with the methods defined in the Myhelloworld class. The precedence is that methods in the current class override the Trait method, and the trait method overrides the method in the base class.

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

The above routines will output:

Hello world!

The above information is from the PHP website manual.

The use of trait in Laravel

The trait feature is used extensively in Laravel to improve the reusability of code, but this article cited an example from a Laravel project.

For example, there is a show method in a pagecontroller.php controller:

Public function Show ($slug) {  $page = Pagerepository::find ($slug);  $this->checkpage ($page, $slug);   Return View::make (' pages.show ', [' page ' = + $page]);

Here the Pagerepository::find () method is the use of a trait method that uses namespace declarations in pagerepository.php and introduces:

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;  Omit 800 Sub} here

Where slugrepositorytrait this trait defines the Find method:

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

This allows the trait to be used in the control, and the reuse of the code is well implemented.

Personal Understanding:

Using trait in a class is equivalent to the class also having properties and methods defined in trait. Traits the use of the scene is if more than one class to use the same properties or methods, this time using traits can easily add these properties or methods to the class, instead of each class to inherit a class, if the inheriting class is a vertical extension of a class, then the traits is a horizontal extension of a class, This enables code reuse.

The use of trait in PHP can also refer to the previous article "PHP Traits simple Use example"

This article turns from: Small Talk blog http://www.tantengvip.com/2015/12/laravel-trait/

More interested in laravel related content readers can view this site topic: "Laravel Framework Introductory and Advanced tutorial", "PHP Excellent Development Framework Summary", "Smarty Template Primer Basic Tutorial", "PHP date and Time usage summary", "PHP object-oriented Programming introduction tutorial "," PHP String Usage Summary "," Introduction to Php+mysql Database Operations "and" PHP common database Operation Skills Summary "

It is hoped that this article is helpful to the PHP program design based on Laravel framework.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.