Use statement

From PhalangerWiki

Jump to: navigation, search

Use statement brings PHP aliasing into Phalanger. It has been enabled since Phalanger/3.0.

Use statement is performed in parse-time, and it has no effect for dynamic constructs.

Syntax

  • use A\B\C as D;
  • use A\B\C; is an equivalent to use A\B\C as C;
  • use A\B\C, D\E\F; allows several aliases to appear within single use statement

Use statement can appear in global scope or namespace scope only, and affects only code within this scope.

  • Note use statement in global scope does not affect code in namespace scopes.
  • Note use statement only affects statements below the use, because it is processed in parse-time.

Usage

Use statement is used to shorten referencing namespaced types. Also it does not affect the performance since the use statement is processed in parse-time only.

Example

namespace A
{
   use A; // import global \A into the current scope
   use System\Collections\Generic as G;

   $a = new A; // equivalent to new \A;
   $b = new B; // equivalent to new \A\B;
   $g = new G\List<:string:>; // equivalent to new \System\Collections\Generic\List<:string:>;
}