Many programmers are using
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, c
- < ? PHP
- $ Color ' green ';
- $ Fruit ' Apple ';
- ?>
- < ? PHP
- function foo ()
- {
- Global $color;
- Include ' vars.php ';
- echo "A $color $fruit";
- }
- Foo ();
- A Green apple
- echo "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 the $color, and does not violate the provisions of (1). 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)
Scope of PHP include scope 2, functions, classes
- < ? PHP
- Class 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 /> ';
- }
- ?>
- < ? PHP
- function Include_class () {
- Include (' classb.php ');
- }
- Include_class ();
- $ OBJB New ClassB ();
- $objB- > printit ();
- Print it in ClassB.
- Show_func_included ()
- show_func_included
- ?>
As you can see from this example :
All functions and classes defined in the contained file are included and have global scope in 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
http://www.bkjia.com/PHPjc/446001.html www.bkjia.com true http://www.bkjia.com/PHPjc/446001.html techarticle Many programmers are using note: This document is based on the include narrative but also applies to require. These two structures are exactly the same except in how they handle containment failures: the inclusion failed ...