About PHP namespaces (combined with code examples, simple and easy to understand)

Source: Internet
Author: User
here is the name of the PHP I have to organize the namespace, interested students can go to see.

First file
cat.class.php

<?phpnamespace Animals\cat;        Class Cat    {public            function speak ()            {                    echo ' miaow ';            }    }        function root ()    {                return ' cat.class.php ';    }? >123456789101112131415

A second file
dog.class.php

<?phpnamespace Animals\dog;        Class Dog    {public        function speak ()        {            echo ' woof ';        }    }    Class Cat    {public        function speak ()        {            echo ' Miaoth ';        }    }    function root () {        return ' dog.class.php ';    }? >1234567891011121314151617181920212223

index.php
A third file, the file that loads the above namespace

<?php namespace Index;        Require_once ' cat.class.php ';        Require_once ' dog.class.php ';        Use Animals\cat;            Use Animals\dog as Snoopi;      Fully qualified name $cat 1 = new \animals\cat\cat ();        $cat 1->speak ();        Echo ' <br/> ';        $cat 2 = new \animals\dog\cat ();        $cat 2->speak ();        Echo ' <br/> ';        $dog 1 = new \animals\dog\dog ();        $dog 1->speak ();            Echo ' <br/> ';        The non-fully qualified name $cat 3 = new Cat\cat ();        $cat 3->speak ();            Echo ' <br/> ';        Aliases * Aliases and non-fully qualified names cannot be used together $dog 3 = new Snoopi\dog ();        $dog 3->speak ();        More than just functions, any available resource under the namespace can invoke [functions, variables, constants, and so on] echo snoopi\root (); Echo ' <br/> ';? >12345678910111213141516171819202122232425262728293031323334 

Namespace classes can be analogous to file directory systems
A class [or function, variable, etc.] of a namespace invokes the contents of a file in a directory
The fully qualified name is the office to find the contents of the file in the path
A non-fully qualified name is equivalent to
Use the ' relative path ' assignment to a variable when using the namespace, which defaults to the last sub-space
Use the AS keyword to set the name of the variable, which is the alias, so aliases and non-fully qualified names cannot be used at the same time (because a use[+as] can only have one alias).
Analogy file directory system:
File location:/root/path/file/filecontent;
Use/root/path/file is file= '/root/path/file′ so the path to file is file= '/root/path/file′ so the path to file is File/filecontent
* Namespace Index in index.php, equivalent to indicating the current file location
So if the contents of the file in the index.php are modified to

<?php    require_once ' cat.class.php ';        Require_once ' dog.class.php ';        Use Animals\cat\cat; #引入该命名空间下的类    $cat 4 = new Cat ();        $cat 4->speak ();    Root ();? >123456789

Use is equivalent to loading only the cat class in the namespace Animals\cat, and the root () function does not ' load '
So the run will hold an error: the root () function is not declared

Above is the namespace I have compiled for you about PHP, I hope it will be helpful to everyone in the future.

Related articles:

PHP namespaces (combined with code detailed answers)

Learn more about the difference between PHP namespaces and automatic loading

To detail the scope in PHP in conjunction with code

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.