84

Recently I came across a class that uses use statement inside of the class definition.

Could someone explain what exactly does it do - as I can't find any information about it.

I understand that it might be a way of moving it away form a global scope of the given file, but does it perhaps allow the given class inherit from multiple parent classes as well - since extends only allows one parent class reference?

The example I saw was in the User model of the original installation of Laravel:

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

}

and I've seen some examples of this model actually using methods included within the UserTrait class - hence my suspicion, but would really like to find out more about the meaning of the enclosed use statements.

PHP documentation says:

The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped. The following example will show an illegal use of the use keyword:

followed by the example:

namespace Languages;

class Greenlandic
{
    use Languages\Danish;

    ...
}

which would indicate that it is an incorrect use of the use keyword - any clues?

Sebastian Sulinski
  • 5,815
  • 7
  • 39
  • 61
  • 9
    If it can easily be answered then point me to that example -don't just downvote without actually giving the answer! – Sebastian Sulinski Sep 27 '14 at 09:03
  • http://php.net/manual/en/language.oop5.traits.php – GordonM Sep 27 '14 at 09:11
  • So that's precisely what I thought - multiple inheritance - 'A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.' - was it really that hard? - oh - and thanks for the link! – Sebastian Sulinski Sep 27 '14 at 09:16
  • It's really not multiple inheritance, it's closer to mixins. – GordonM Sep 27 '14 at 09:21
  • Understood - although functionally it allows inheritance of additional methods - even with the similar overwriting mechanism. – Sebastian Sulinski Sep 27 '14 at 09:41
  • The general term for this would be composition, or as Head First's Design Patterns puts it : "Instead of inheriting their behaviour, the [...] gets their behaviour by being composed with the right behaviour object.". Traits additionally gives you the advantage of providing you "direct access" to the functions which comes with the Traits. – sbrattla Jun 01 '21 at 07:55

1 Answers1

64

They are called Traits and are available since PHP 5.4. They are imported into another class or namespace using use keyword which is included since PHP 5.0 like importing a regular class into another class. They are single inheritance. The primary reason for the implementation of traits is because of the limitation of single inheritance.

For more details see the PHP trait manual:

Wolverine
  • 1,712
  • 1
  • 15
  • 18
Sagar Rabadiya
  • 4,126
  • 1
  • 21
  • 27