PHP Global variables and classes with a deep understanding _php tutorial

Source: Internet
Author: User
Scenario 1:
Father.php is defined as follows:
Copy CodeThe code is as follows:
$jack = 1000;
?>
Children.php is defined as follows:
Require ("father.php");
$jack = 123;
echo $jack. " /n ";
?>

PHP children.php
The run output is 123.
If $jack=123 is commented out, it runs at 1000, and if $jack=123 is dropped to require ("father.php"), the run result is 1000.
Good understanding: PHP Interpretation of the implementation, explain to where, where to execute. Like $jack, this is a global variable, such as the first case of the initial use of it is 1000, is in the Require
, the result is changed to 123, so the output of the running result is 123.
Scenario 2:
Change the children.php code to read as follows:
Copy CodeThe code is as follows:
Require ("father.php");
function Testjack () {
if (!isset ($jack)) {
Echo ' $jack is null '. " /n ";
}
}//testjack
Testjack ();
?>

PHP children.php
The results are: $jack is null. This means that the $jack referenced in Testjack () is a local variable.
If you use the Global keyword, declare that the $jack is a global variable and the code changes to the following:
Copy CodeThe code is as follows:
Require ("father.php");
function Testjack () {
Global $jack;
if (!isset ($jack)) {
Echo ' $jack is null '. " /n ";
}else{
Echo ' $jack is not null '. " /n ";
}
}//testjack
Testjack ();
?>

The result of the operation is $jack are not null!
Scenario 3:
The children.php code is as follows:
Copy CodeThe code is as follows:
Require ("father.php");
Class jacktest{
Public Function Testjack () {
if (!isset ($jack)) {
Echo ' $jack is null '. " /n ";
}else{
Echo ' $jack is not null '. " /n ";
}
}//testjack
}
$jackTest = new Jacktest ();
$jackTest->testjack ();
?>

Run result output: $jack is null
This is because the $jack of this function in class is a local variable.
If you add global $jack at the start of function testjack, then the output $jack is not null.
Relatively easy to understand.
Scenario 4:
Load the file name as a parameter dynamically, the code is as follows:
Copy CodeThe code is as follows:
$casefile = $_server[' argv '][1];
echo $casefile. " /n ";
Require ($casefile);
echo $jack. " /n ";
?>

Run PHP children.php father.php
The results are as follows:
father.php
1000
This means that our dynamic loader is running successfully.
Scenario 5:
To combine dynamic loading with the definition of a class:
Directory-Relational:
|-c.php
|-bfold-b.php
|-afold-a.class.php (the function inside refers to the: /bfold/b.php)
That is to say, in c.php new class A.class, and a.class.php a function require Bfold folder b.php, this require (.. /bfold/b.php) error, Warning: ...
Because you let the server is currently executing the c.php file, so PHP parsing is the path relative to the c.php, you try to put (.. /bfold/b.php) Change to (bfold/b.php) look, should not error.
The following is an example of a program that uses require_once (a.php) inside a function.
understanding of the require_once:
Suppose require_once (a.php) is referenced in b.php; This statement:
Then it is the equivalent of calling a.php, the anonymous lambda function to execute. Such as:
C.php in a function call require the b.php------"
b.php require the a.php--------in the ordinary statement
a.php
Now we call PHP b.php, because b.php uses require in the normal statement to call a.php, then a.php will be a variable of its relative a global variable, registered in the b.php environment. Because b.php is the root to start calling the file, his environment is the global environment. Therefore, the variables in the a.php file can be used normally in b.php.

Now we call PHP c.php, then C is the function using require call b.php, and then B called a, feel in the process of this call, relative B and A root run environment is C call function environment, but C call function if you want to use B and The variable in a, there is no way out.

If you use global $a to reference, then because $a in this case does not belong to the global variable, the reference is not.
If you use $a to refer to, the $a is not referenced as a local variable.

http://www.bkjia.com/PHPjc/327466.html www.bkjia.com true http://www.bkjia.com/PHPjc/327466.html techarticle The case 1:father.php is defined as follows: the copy Code code is as follows. PHP $jack = $;? children.php as defined below: PHP require ("father.php"); $jack =123; Echo $j Ack. " /n ";? PHP childr ...

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