Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Use In Traits

use imports a trait in the current structure, either another trait, a class or an enumeration.

use is a keyword for traits and classes.

The import federates all the defined structures in both traits and classes: the methods, the properties and the constants.

There may be naming conflicts between the imported traits and the host structure: the conflicts may be between an import and the host structure, or between two distinct imported traits.

In that case, there is a mechanism to resolve the conflicts: the insteadof keyword allows the host to choose between several competing implementations.

The as keyword is also available to create aliases from the import structures. These aliases shall not introduce new naming conflicts. It is not possible to use abstract as an alias when resolving names with traits.

<?php

    trait Trunk {
        function trumpet() {}
    }

    trait Tusk {
        use Trunk {
            Trunk::trumpet as goo; // aliasing
            Trunk::trumpet insteadof trumpet; // aliasing
            Tusk::trumpet as hoo; // aliasing
        }

        function trumpet() {}
    }

?>

Documentation

See Also