php global variables and classes work well together

  • 2020-06-07 04:06:13
  • OfStack

Case 1:
father.php is defined as follows:
 
<?php 
$jack = 1000; 
?> 
children.php  The following definitions : 
<?php 
require("father.php"); 
$jack=123; 
echo $jack."/n"; 
?> 

php children.php
The run output is 123.
If you comment out $jack=123 and run it at 1000, if you put $jack=123 on require(" father.php "); Previously, the running result was 1000.
php explains execution, explains to where, executes to where. For example, $jack is a global variable. For example, in case 1, the initial use of it is 1000, which is in require
", the result is changed to 123, so the result is 123.
Situation 2:
children.php code changed as follows:
 
<?php 
require("father.php"); 
function testJack(){ 
if(!isset($jack)){ 
echo '$jack is null'."/n"; 
} 
}//testJack 
testJack(); 
?> 

php children.php
The result is $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 change the code to read 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(); 
?> 

The result is $jack is not null!
Case 3:
children. php code is 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
That's because the $jack of this function in class is a local variable.
Add global $jack to the beginning of function testJack; So $jack is not null.
It's easier to understand.
Situation 4:
The file name is dynamically loaded as a parameter. The code is as follows:
 
<?php 
$casefile = $_SERVER['argv'][1]; 
echo $casefile."/n"; 
require($casefile); 
echo $jack."/n"; 
?> 

Run php children.php ES74en.php
The results are as follows:
father.php
1000
That means our dynamic loader ran successfully.
Situation 5:
To combine dynamic loading with class definition:
The directory relationship is as follows:
|- c.php
|- Bfold - b.php
|-Afold-a.class.php (the inside function refers to.. / Bfold b. php)
That is to say, in c. php new class a. class, and a. class. php, b. php, this require (.. /Bfold/b) error, Warning:...
Since you have the server currently running c.php, php parses the path relative to c.php. Bfold/ b.php) (Bfold/ b.php)
Here is a program example of using require_once (ES131en.php) inside a function.
Understanding of require_once:
Suppose require_once(ES141en.php) is cited in ES137en.php. This statement...
So you're essentially calling A.php, the anonymous lambda function. The diagram below:
C. php in 1 function call B. php--
B.php.
A.php
Now let's call php B.php; Since B.php calls A.php using require in a normal statement, A.php registers its variable as a global relative to A. Because B.php is the root start calling file, its runtime environment is the global environment. So the variable in A.php file can be used normally in ES179en.php.

Now let's call php C.php; Then C calls ES188en.php using require, then B calls A, feeling that during this call, relative to B and A root run environment is the calling function of C, but if the calling function of C USES the variables in B and A, there is no way.

If you use global $a to reference, then $a cannot be referenced because it is not a global variable in this case.
If you use $a to reference, $a will not be referenced because it is treated as a local variable.

Related articles: