PHP fallback global functions/constants

Source: Internet
Author: User
In a namespace, when PHP encounters an unqualified class, function, or constant name, it uses a different precedence policy to resolve the name. The class name always resolves to the name in the current namespace. Therefore, you must use a fully qualified name when accessing a class name that is inside the system or that is not contained in a namespace, for example:

Example #1 Accessing the global class in the namespace

<?php    namespace a\b\c;    Class Exception extends \exception {}    $a = new Exception (' Hi ');//$a is an object of class a\b\c\exception    $b = new \exception (' Hi '); $b is an object of class Exception    $c = new Arrayobject;//Fatal error, A\b\c\arrayobject class not found?>

For functions and constants, if the function or constant does not exist in the current namespace, PHP will fallback to use the function or constant in the global space.

Example a fallback global function/constant in #2 namespace

<?php    namespace a\b\c;    Const E_ERROR =;    function strlen ($str)    {        return \strlen ($STR)-1;    }    echo E_error, "\ n"; Output "Ini_all"    echo, "\ n",//Output "7"-use global constants Ini_all    echo strlen (' Hi '), "\ n",//Output "1"    if (Is_array (' Hi ')) {//output "is not an array"        echo "is array\n";    } else {        echo "was not array\n";    }? >
  • 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.