PHP Global variables and classes with a deep understanding of _php skills

Source: Internet
Author: User
Situation 1:
Father.php is defined as follows:
Copy Code code as follows:

<?php
$jack = 1000;
?>
Children.php is defined as follows:
<?php
Require ("father.php");
$jack = 123;
echo $jack. " /n ";
?>

PHP children.php
Run the output to 123.
If the $jack=123 is commented out, run to 1000, if the $jack=123 is placed to require ("father.php"), the result is 1000.
Better understanding: PHP Explain execution, explain to where, execution to where. Like $jack This is a global variable, as the first case of the initial use of it is 1000, is in require
, the result is changed to 123, so the output of the run result is 123.
Situation 2:
Replace the children.php code with the following:
Copy Code code as follows:

<?php
Require ("father.php");
function Testjack () {
if (!isset ($jack)) {
Echo ' $jack is null '. ' /n ";
}
}//testjack
Testjack ();
?>

PHP children.php
The result of the run was: $jack is null. That is, 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 should read as follows:
Copy Code code as follows:

<?php
Require ("father.php");
function Testjack () {
Global $jack;
if (!isset ($jack)) {
Echo ' $jack is null '. ' /n ";
}else{
Echo ' $jack is not null '. ' /n ";
}
}//testjack
Testjack ();
?>

Then the run result is $jack isn't null!
Situation 3:
The children.php code is as follows:
Copy Code code as follows:

<?php
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 the global $jack at the beginning of the function Testjack, then the $jack is not NULL is output.
Relatively easy to understand.
Situation 4:
Dynamically load the filename as a parameter, the code is as follows:
Copy Code code as follows:

<?php
$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.
Situation 5:
To combine dynamic loading with the definition of a class:
Directory relationships such as:
|-c.php
|-bfold-b.php
|-afold-a.class.php (the function in the inside quote ...) /bfold/b.php)
That is, the new class A.class in c.php, and a function of a.class.php require bfold in the b.php folder, the Require (... /bfold/b.php) error, Warning: ...
Because you let the server is currently executing the c.php file, so the PHP parsing is the path relative to the c.php, you try to put (. /bfold/b.php) changed to (bfold/b.php) look, should not be an error.
The following is a program example that uses require_once (a.php) inside a function.
The understanding of require_once:
Suppose the require_once (a.php) is quoted in the b.php; This statement ...
So it's actually equivalent to calling the anonymous lambda function of a.php to execute. The following figure:
C.php in a function call require the b.php------"
B.php in ordinary sentences require a.php--------"
a.php
Now we call PHP b.php because b.php uses the require to call a.php in a normal statement, then a.php registers its relative a variable as a global variable, registering it in the b.php environment. Because b.php is the root of the call file, his running 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 called b.php in the function using require, and B then calls a, feeling that in the process of this call, the relative B and a root runtime environment is the calling function of C, but the call function of C is to use B and The variables in a, there is no way.

If you use global $a to refer to, the reference is not available because $a is not part of the global variable in this case.
If you use $a to refer to, the $a will not be referenced as a local variable.

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.