How to correctly understand PHP include scope _php tutorial

Source: Internet
Author: User
Tags vars
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

 
  
  
  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. ?>

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

 
 
  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. ?>

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 ...

  • 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.