How to correctly understand the PHP include Scope

Source: Internet
Author: User

Many programmers are using

Note: This document is based on the include description, but also applies to require. These two structures are exactly the same except for how to handle include failures: When the include () fails, include () generates a warning and continues to execute, while require () this results in a fatal error. In other words, if you want to stop processing the page when a file is lost, use require (); otherwise, use include ().

PHP include Scope 1, c

 
 
  1. < ?php  
  2. $color = 'green';  
  3. $fruit = 'apple';  
  4. ?> 
 
 
  1. < ?php  
  2. function foo()  
  3. {  
  4. global $color;  
  5. include 'vars.php';  
  6. echo "A $color $fruit";  
  7. }  
  8. foo();   
  9. // A green apple  
  10. echo "A $color $fruit";   
  11. // A green  
  12. ?>  

From this example, we can see that:

(1) The PHP include scope of the variable containing the file complies with the scope of the included file. That is, use include in the function to include the variables of other files. These variables are in the scope of this function.

(2) The value of $ color can be printed out of the foo () function without violating the rules of (1. That's because the function has declared $ color as a global (although there is no $ color variable outside of the foo () function, the $ color variable is not vars. the $ color variable in php is a new variable that is forcibly declared as "Global". At this time, it is not assigned a value. When it is included in vars. after php, according to the (1) principle, vars. the $ color variable in php automatically has the scope in the function, so its value is the value of the global variable $ color)

PHP include scope 2. Scope of functions and Classes

 
 
  1. < ?php  
  2. class ClassB {  
  3. /**  
  4. * constructor  
  5. */  
  6. public function __construct(){}  
  7. /**  
  8. * destructor  
  9. */  
  10. public function __destruct() {}  
  11. public function printit() {  
  12. echo 'print it in ClassB.<br />';  
  13. }   
  14. }  
  15. function show_func_included() {  
  16. echo 'show_func_included<br/>';  
  17. }  
  18. ?> 
 
 
  1. < ?php  
  2. function include_class() {  
  3. include('classb.php');  
  4. }  
  5. include_class();  
  6. $objB = new ClassB();  
  7. $objB->printit();   
  8. // print it in ClassB.  
  9. show_func_included()   
  10. // show_func_included  
  11. ?>  

From this example, we can see that:

All functions and classes defined in the contained files have a global scope after they are included.

Conclusion:

1. the PHP include scope of the variable containing the File follows (does not change) The scope of the included file.
2. All functions and classes defined in the contained files have a global scope after they are included.


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.