Php global variables and classes

Source: Internet
Author: User
Php global variables and classes are used in combination for a deep understanding. Scenario 1: father. php is defined as follows: Copy the code as follows :? Php $ jack1000 ;? Children. php is defined as follows :? Phprequire (father. php); $ jack123; echo $ jack. n ;? Phpchildr Case 1:
Father. php is defined as follows:

The code is as follows:


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


Php children. php
The output is 123.
If you comment out $ jack = 123 and run it as 1000, if you put $ jack = 123 in require ("father. php");, the running result is 1000.
Better understanding: php explains how to execute, where to explain, and where to execute .. $ Jack is a global variable. for example, in the first case, 1000 is used for initial use.
And the result is changed to 123. Therefore, the output result is 123.
Case 2:
The children. php code is changed to the following:

The code is as follows:


Require ("father. php ");
Function testJack (){
If (! Isset ($ jack )){
Echo '$ jack is Null'. "/n ";
}
} // TestJack
TestJack ();
?>


Php children. php
The running result is: $ jack is null. that is to say, $ jack referenced in testJack () is a local variable.
If the global keyword is used, declare $ jack as a global variable. the code is changed to the following:

The 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 running result is $ jack is not null!
Case 3:
The children. php code is as follows:

The 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 ();
?>


Output result: $ jack is null
This is because $ jack of this function in class is a local variable.
If global $ jack is added at the beginning of function testJack, $ jack is not null is output.
It is easier to understand.
Case 4:
Dynamically load the file name as a parameter. the code is as follows:

The code is as follows:


$ Casefile = $ _ SERVER ['argv'] [1];
Echo $ casefile. "/n ";
Require ($ casefile );
Echo $ jack. "/n ";
?>


Run php children. php father. php
The result is as follows:
Father. php
1000
That is to say, the dynamic loading program runs successfully ..
Case 5:
Dynamic loading and class definition should be combined:
The directory relationship is as follows:
|-C. php
|-Bfold-B. php
|-Afold-a. class. php (the function in it references ../Bfold/B. php)
That is to say, in c. new class a in php. class, while. class. in a function of php, require B in the Bfold folder. php, this require (.. /Bfold/B. php) error, Warning :......
Because you want the server to execute c. php file, so php parses the path relative to c. for php, try (.. /Bfold/B. php) to (Bfold/B. php) check it and you will not report an error.
The following is an example of A program that describes the use of require_once (A. php) in the function ).
Understanding of require_once:
Suppose B. php references require_once (A. php ..
In fact, it is equivalent to calling the anonymous lambda function A. php for execution. For example:
C. php require B. php ------ in a function call
B. php require A. php -------- in common statements
A. php
Now we call php B. php; because B. php uses require to call. php, then. php registers the global variable relative to A to B. php environment. Because B. php is the root and starts to call files, its runtime environment is the global environment. Therefore, the variables in the. php file can be used normally in B. php.

Now we call php C. php; then C calls B. php, and then B calls A again. it seems that during this call, the runtime environment of B and A is the environment of C's function call, but if C calls A function to use the variables in B and A, there is no way.

If global $ a is used for reference, $ a cannot be referenced because it is not a global variable.
If $ a is used for reference, $ a cannot be referenced as a local variable.

Father. php is defined as follows :? Php $ jack = 1000 ;? Children. php is defined as follows :? Php require ("father. php"); $ jack = 123; echo $ jack. "/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.