Get PHP Source code
In order to learn the implementation of PHP, the first need to download PHP source code. Download source preferred is to PHP official website http://php.net/downloads.php Download, if you like to use Svn/git and other version control software, you can also use Svn/git to get the latest source code.
# git official address git clone https://git.php.net/repository/php-src.git # can also access GitHub official image git clone git://github.com/php/ Php-src.git cd php-src && git checkout PHP-5.3 # Check out 5.3 branches
PHP Source directory structure
The saying goes: No front, big Qiao not work. PHP's source code is structurally clear. The following is a brief introduction of the PHP source directory structure.
/readme. Php4-to-php5-thin-changes This document details some of the differences between PHP4 and PHP5.
There is a more important file/coding_standards, if you want to write a php extension, this file must read, no matter what your personal code style, how to use indentation and braces, since came to such a group should adapt to such norms, This makes it easier to read code or someone else's reading your code.
As the name implies, here are some of the main files related to source code compilation, such as the beginning of the construction of the buildconf script and other files, as well as a number of scripts to check the environment.
ext Official extension directory, including most of the functions of PHP definition and implementation, such as the array series, PDO series, SPL series, such as the implementation of functions, are in this directory. Individual written extensions can also be placed in this directory for testing and debugging purposes.
Main Here is the most important PHP file, the main implementation of PHP basic facilities, here and Zend engine is not the same, Zend engine mainly to achieve the language of the most core language operating environment.
The implementation directory of the Zend Zend engine, such as lexical parsing of scripts, implementation of opcode and implementation of extension mechanisms, etc.
pear "PHP extension and Application Repository", containing the core files of pear.
The SAPI contains code for various server abstraction layers, such as Apache mod_php,cgi,fastcgi and FPM, among other interfaces.
TSRM PHP's thread safety is built on top of the TSRM library, and the common *g macros in PHP implementations are typically TSRM wrappers, TSRM (thread safe Resource Manager) threading Security Explorer.
tests A collection of test scripts for PHP, including test files for PHP features
Win32 This directory mainly includes some implementations related to the Windows platform, such as the SOKCET implementation under Windows and the *nix platform, and also includes the PHP-related scripts compiled under Windows.
global variable macros in PHP
In the source code of PHP often see some of the very common macros, or some of the people who are only beginning to contact the source of the more difficult to understand. The code in the PHP source of the frequency is very high, basic in each module will have their figure.
In PHP code often see a number of functions like PG (), EG (), they are defined in PHP macros, the main role of this series is to solve the thread safety of the global variables written by the package macro, such as $php_src/main/php_ Many of these macros are included in the Globals.h file. For example PG this PHP core global variable macro. The code is defined as follows.
#ifdef ZTS //compile-time thread-safe is used thread Safety Library # define PG (v) tsrmg (core_globals_id, php_core_globals *, v) extern Phpapi int CORE_GL obals_id; #else # define PG (v) (CORE_GLOBALS.V)//Otherwise this is actually an ordinary global variable extern zend_api struct _php_core_globals core_globals; #endif
As above, Zts is a thread-safe tag, which is described in detail in a later section, and is no longer explained here. The following is a brief explanation of some of the global parameters of the PHP runtime, the global variable being a struct, the meaning of each field, such as the comment after the field:
struct _php_core_globals {zend_bool magic_quotes_gpc;//Whether automatic string escaping is used for the input Get/post/cookie data. Zend_bool Magic_quotes_runtime; Whether to use automatic string escape for data generated from external resources at runtime Zend_bool magic_quotes_sybase; Whether to use the automatic string escaping Zend_bool safe_mode in Sybase form; Whether to enable Safe mode Zend_bool allow_call_time_pass_reference; Whether to force arguments to be passed by reference at function call Zend_bool Implicit_flush; Whether the PHP output layer is required to automatically refresh the data after each output block long output_buffering; Output buffer size (bytes) char *safe_mode_include_dir; In Safe mode, the Uid/gid check is skipped when the files under this group directory and its subdirectories are included. Zend_bool Safe_mode_gid; In Safe mode, the default when accessing the file will do a UID comparison check zend_bool Sql_safe_mode; Zend_bool Enable_dl; Whether to allow the use of the DL () function. The DL () function is only valid if PHP is installed as an Apache module. Char *output_handler; Redirects the output of all scripts to an output handler function. Char *unserialize_callback_func; If the deserializer needs to instantiate an undefined class, the callback function specified here will be called by Unserialize () as the name of the undefined class, long serialize_precision; The precision (number of significant digits) when storing floating-point and double-precision data in a serialized format. Char *safe_mode_exec_dir; In safe mode, only executable programs in this directory allowExecuted by the function executing the system program. Long Memory_limit; The maximum number of bytes of memory that a script can request (using K and M as units). Long Max_input_time; The maximum allowable time (in seconds) for each script to parse the input data (POST, GET, upload). Zend_bool track_errors; Whether to save the most recent error or warning message in the variable $php_errormsg. Zend_bool display_errors; Whether to display the error message as part of the output. Zend_bool display_startup_errors; Whether to display PHP startup errors. Zend_bool log_errors; Whether the error is recorded in the log file, where the specific record depends on the error_log instruction long Log_errors_max_len; Sets the maximum length of the error source that is attached in the error log that is associated with the error message. Zend_bool ignore_repeated_errors; Whether to ignore duplicate error messages when logging error logs. Zend_bool Ignore_repeated_source; Whether duplicate error sources are ignored when repeating error messages are ignored. Zend_bool Report_memleaks; Whether to report a memory leak. Char *error_log; The file to which the error log is logged. Char *doc_root; The "root directory" of PHP. Char *user_dir; Tell PHP which directory to go to when using/~username to open the script to find Char *include_path; Specifies a set of directories for require (), include (), Fopen_with_path () functions to find files. Char *open_basedir; Restrict all files (including the file itself) that PHP allows to operate under this Group directory list. Char *extension_dir; The directory in which the extension library (module) is stored, i.e. PHP is used toFind the directory for the Dynamic expansion module. Char *upload_tmp_dir; Temporary directory of files when uploading files long upload_max_filesize; Maximum size of files allowed to be uploaded. Char *error_append_string; The string char *error_prepend_string for the output after the error message; String char *auto_prepend_file for output before error message; Specifies the file name that is automatically resolved before the primary file. Char *auto_append_file; Specifies the file name that is automatically parsed after the primary file. Arg_separators Arg_separator; The delimiter used to separate parameters in the URL generated by PHP. Char *variables_order; PHP registers the order of environment, GET, POST, Cookie, and Server variables. HashTable Rfc1867_protected_variables; RFC1867 protected variable name, useful in main/rfc1867.c file to this variable short connection_status; Connection status, there are three states, normal, interrupt, timeout short ignore_user_abort; Whether the entire request is persisted even after the user aborts the request. unsigned char header_is_being_sent; Whether the header information is being sent zend_llist tick_functions; It is only useful in the PHP_TICKS.C file in the main directory, where the functions defined here are useful in functions such as register_tick_function. Zval *http_globals[6]; Store get, POST, server and other information Zend_bool expose_php; Whether to show PHP information Zend_bool register_globals; IsThe E, G, P, C, and S variables are registered as global variables. Zend_bool register_long_arrays; Whether to enable legacy long-array (http_*_vars). Zend_bool register_argc_argv; Whether to declare $ARGV and $ARGC global variables (containing information using the Get method). Zend_bool Auto_globals_jit; is created only when using the $_server and $_env variables (not automatically when the script is started). Zend_bool y2k_compliance; Whether to force open 2000 adaptation (which may cause problems in non-Y2K adapted browsers). Char *docref_root; If the html_errors instruction is turned on, PHP will display the hyperlink on the error message, Char *docref_ext; Specifies the file name extension (must contain '. '). Zend_bool html_errors; Whether to use HTML tags in the error message. Zend_bool xmlrpc_errors; Long Xmlrpc_error_number; Zend_bool Activated_auto_globals[8]; Zend_bool modules_activated; Whether the module Zend_bool file_uploads has been activated; Whether to allow HTTP file uploads. Zend_bool During_request_startup; Whether to Zend_bool Allow_url_fopen during the initialization of the request; Whether to allow open remote files Zend_bool always_populate_raw_post_data; Whether the $http_raw_post_data variable is always generated (raw POST data). Zend_bool Report_zend_debug; Whether to open Zend Debug, only used in the Main/main.c file. INT Last_error_type; The last error type char *last_error_message; The last error message char *last_error_file; Final error file int last_error_lineno; Last error line char *disable_functions; The directive accepts a comma-delimited list of function names to disable a specific function. Char *disable_classes; The directive accepts a comma-delimited list of class names to disable a particular class. Zend_bool Allow_url_include; Whether to allow Include/require remote files. Zend_bool exit_on_timeout; Timeout then exit #ifdef php_win32 zend_bool com_initialized; #endif long max_input_nesting_level; The maximum number of nesting layers zend_bool in_user_include; Whether the user contains space char *user_ini_filename; The INI file name for the user is long User_ini_cache_ttl; INI cache expiration limit char *request_order; The priority is higher than variables_order, which is used when the request variable is generated, and the individual feels that it is a historical problem zend_bool mail_x_header; Used only in ext/standard/mail.c files, char *mail_log; Zend_bool In_error_log;};
A large portion of the above fields correspond to the configuration items in the php.ini file. These fields are assigned when PHP starts and reads the php.ini file, and some of the configuration of the Ini_get () and Ini_set () functions of the user space operate on the global variable.
In other parts of the PHP code there are also many similar macros, these macros and PG macros, are for thread security encapsulation, while the contract by the G-name to indicate that this is global, is generally an abbreviation, because these global variables are used throughout the code, which is also reduced keyboard input. We should all be as lazy as possible, right?
If you have read some of the PHP extensions you should have seen similar macros, which is also a code specification, global variables are best used to name and package this way when writing extensions, because we cannot make any assumptions about the user's PHP compilation conditions.