International PHP Conference Spring 2015
    Edit Report a Bug

    Namespaces

    Table of Contents

    • Namespaces overview
    • Defining namespaces
    • Declaring sub-namespaces
    • Defining multiple namespaces in the same file
    • Using namespaces: Basics
    • Namespaces and dynamic language features
    • namespace keyword and __NAMESPACE__ constant
    • Using namespaces: Aliasing/Importing
    • Global space
    • Using namespaces: fallback to global function/constant
    • Name resolution rules
    • FAQ: things you need to know about namespaces
    spacer add a note

    User Contributed Notes 3 notes

    up
    down
    42
    Anonymous
    3 years ago
    The keyword 'use' has two different applications, but the reserved word table links to here.

    It can apply to namespace constucts:

    file1:
    <?php namespace foo;
      class
    Cat {
        static function
    says() {echo 'meoow';}  } ?>

    file2:
    <?php namespace bar;
      class
    Dog {
        static function
    says() {echo 'ruff';}  } ?>

    file3:
    <?php namespace animate;
      class
    Animal {
        static function
    breathes() {echo 'air';}  } ?>

    file4:
    <?php namespace fub;
      include
    'file1.php';
      include
    'file2.php';
      include
    'file3.php';
      use
    foo as feline;
      use
    bar as canine;
      use
    animate;
      echo \
    feline\Cat::says(), "<br />\n";
      echo \
    canine\Dog::says(), "<br />\n";
      echo \
    animate\Animal::breathes(), "<br />\n"?>

    Note that
    felineCat::says()
    should be
    \feline\Cat::says()
    (and similar for the others)
    but this comment form deletes the backslash (why???)

    The 'use' keyword also applies to closure constructs:

    <?php function getTotal($products_costs, $tax)
        {
           
    $total = 0.00;
           
           
    $callback =
                function (
    $pricePerItem) use ($tax, &$total)
                {
                   
                   
    $total += $pricePerItem * ($tax + 1.0);
                };
           
           
    array_walk($products_costs, $callback);
            return
    round($total, 2);
        }
    ?>
    up
    down
    -29
    php at lanar dot com dot au
    9 months ago
    Be warned that it is not trivial to convert a project to use namespaces. If you add the same namespace to the top of all your files, your application will break.
    All objects in the global namespace, such as ArrayObject, must have a backslash (\) prepend to them.
    If you use get_class() and instanceof, you will have to make adjustments here to.
    If you have an autoloader, you will need to change its behavior.
    It can be done, but subtle errors can creep in and are tedious to rectify.
    up
    down
    -62
    guido at go-mobile dot be
    1 year ago
    Strange, it worked only when I removed the backslash before feline.
    So like this is OK : echo feline\Cat::says()
    spacer add a note
    spacer
    gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.