"Recommended" how to correctly understand the include,include_once,require,require_once of PHP including scope

Source: Internet
Author: User
Tags php language vars

Some of the issues we summarize for you in the PHP include scope include: PHP include variable scope and functions , scope of the class , and so on. We hope to help you.

Many programmers use the PHP language for practical programming, usually only focus on the PHP function can achieve what function, but do not go deep to understand the function of the specific meaning and hidden problems. Today we will introduce you to the issues related to PHP include scopes.

Note: This document is based on the include narrative but also applies to require. These two structures are exactly the same in addition to how they handle containment failures: When inclusion fails, the include () generates a warning and continues execution, while require () causes a fatal error. In other words, if you want to stop processing a page when you encounter a lost file, use require (), or include ().

PHP include scope 1: Scope of variables

<?php$color = ' green '; $fruit = ' Apple ';? ><?phpfunction foo () {global $color; include ' vars.php '; echo "A $color $fruit";} Foo (); A green Appleecho "a $color $fruit"; A green?>

As you can see from this example:

(1). The PHP include scope of the variable containing the file conforms to the scope where the containing file resides. That is, using include in the function to include the variables of other files, the scope of these variables is within the function .

(2). The. Foo () function can print out the value of $color, and does not violate the (1) rule. That's because the function has already declared $color as global (although there is no $color variable outside of the Foo () function, and the $color variable is not a $color variable in vars.php, but a new variable that is forced to declare "global," which is not yet assigned. When the following is included in the vars.php, according to the principle of (1), the $color variable in the vars.php automatically enjoy the scope within the function, so its value is the value of the global variable $color)

PHP include scope 2: function, scope of class

<?phpclass ClassB {/** * constructor */public function __construct () {}/** * destructor */public function __destruct () {}public function printit () {echo ' Print it in classb.<br/> ';}} function show_func_included () {echo ' show_func_included<br/> ';}? ><?phpfunction Include_class () {include (' classb.php ');} Include_class (); $objB = new ClassB (); $objB->printit (); Print it in classb.show_func_included ()//show_func_included?>

All functions and classes defined in the contained file are included, with the global scope in the containing file (equivalent to attaching the " function ", " class " code to the header of the containing file).

Conclusion:
1. The PHP include scope of the variable containing the file follows (does not change) the scope at which the containing file resides.
2. All functions and classes defined in the contained file are included and have global scope in the containing file.

"Recommended" how to correctly understand the include,include_once,require,require_once of PHP including scope

Related Article

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.