PHP source code for PHP developers-Part 1-source code structure and developer source code

Source: Internet
Author: User
Tags php source code

PHP source code for PHP developers-Part 1-source code structure and developer source code

Article from: http://www.aintnot.com/2016/02/04/phps-source-code-for-php-developers-ch

Original article: http://blog.ircmaxell.com/2012/03/phps-source-code-for-php-developers.html

As a developer, I found that more and more PHP source code is being viewed in my daily work. It is very useful to understand what happened behind the scenes in order to find out the strange boundary problems and why some problems should have occurred but have not happened. It is also useful when documents are missing, incomplete, or incorrect. Therefore, I have decided to share my learned knowledge through a series of articles and give PHP developers enough knowledge to truly read the C language source code of PHP. You do not need to have the C language basics (We will summarize some basics), but it will be more helpful if so.

This is the first article in this series. In this article, we will talk about the basics of a PHP program: where to find it, the basic code structure, and some basic C language concepts. It should be noted that the goal of this series of articles is to get the reading and understanding ability of the source code. This means that some concepts will be simplified, not too complex descriptions, to take a look at some points. This will not cause obvious differences for reading, but if you want to contribute to the source code, you need to add more knowledge. When I simplify it, I will try to point it out.

In addition, this series of articles is based on the source code of version 5.4. In different versions, most of the concepts are the same, but here, we need to define a version for this article (in order to make the next article easier to follow after the new version is released ).

So, can we start?

Where can I find the PHP source code?

The simplest way to download the PHP source code is through the php svn repository. For this article, we checked out the 5.4 branch. This is great for PHP Development (solving bugs, implementing features, etc. It is worth noting that the PHP community is migrating the source code to the GIT repository (when this article is being written. Once the migration is complete, I will update this article to meet the standards. (Note: PHP has been migrated to the GIT repository during the translator's translation ).

In fact, downloading source code is not really useful for our purposes. We don't want to edit it. We just want to use it and track how it runs. We can download it and import it to a good IDE. In these ides, We can click to jump to the definition and declaration of functions. When I find this is slightly more difficult than I think. I have a better solution.

It turns out that the PHP community is maintaining a very good tool for us. That is lxr.php.net. This is mainly a list of automatically generated searchable source code, with syntax highlighting and function linking. This is a tool that I almost only use to browse the C source code. It is really great (even when I write a patch, I still go to lxr instead of the code library I am developing ). We will not talk about how to make more effective searches, but we will talk about PHP core functions.

From here on, we will start to talk about PHP5.4. To achieve this, we will use this lxr link as the basis for other articles. When I mention the "5.4 root directory", I mean this page.

Now that we can view the source code directory, let's talk about what is in it.

PHP source code structure

Then, when you view the files and directories listed in the 5.4 root directory, there are a lot to study. I hope you only pay attention to two directories: ext and Zend. Other files and directories are important for PHP extension and development, but we can ignore them for our purposes. So why are these two directories so important?

PHP programs are divided into two main parts, you guessed it. The first part is the Zend engine, which controls the runtime environment of PHP code. It handles all the "language layer" features provided by PHP, including: variables, expressions, Syntax Parsing, code execution, and error handling. Without this engine, PHP is not available. The source code of the engine is placed in the Zend directory.

The second core of PHP is the extension included in PHP. These extensions include every core function that we can call in PHP (for exampleStrpos,Substr,Array_diff,Mysql_connectAnd so on ). It also includes the core class (MySQLi,SplFixedArray,PDOAnd so on ).

In the core code, the easiest way to find the function you want to view is to view the PHP document homepage. PHP documentation is also divided into two main parts (for our purpose), language reference and function reference. As a huge summary, if you want to view the definition in the language reference, you may find it in the Zend folder. For function reference, you can find it in the ext folder.

Some basic C language concepts

This part is not for getting started with C, but a "Reader's Guide ". It has the following concepts:

Variable

In C, variables are static and strongly typed. This means that the variable must be defined by a type before it can be used. Once defined, you cannot change its type (you can convert it to another type later, but you need to implement it using different variables ). Because, in C, variables do not actually exist. They are just for the convenience of memory address tags we use. Because of this, the C language is not referenced in PHP. Instead, it has a pointer. For our purpose, we can think of a pointer as a variable pointing to another variable. Use it as a variable in PHP.

Then, let's talk about the variable syntax through the above description. C language does not use any prefix to identify variables. Therefore, the only way to express their differences (to achieve our goal) is to view their definitions. If you see the character after the type and space at the top of the function (or the declaration of the function), it is the variable. One key point to note is that there can be one or more symbols before the variable name. Asterisk (*) indicates that the variable is a pointer to a certain type (a reference ). The two asterisks indicate that the variable is a pointer to the pointer. The three asterisks indicate that the variable points to a pointer to another pointer.

This indirect addressing is very important because PHP uses many double-layer pointers internally. This is because the engine needs to be able to pass block data (PHP variables), and all interesting types such as PHP reference, copy at write time, and object reference. Therefore, just realize that ** ptr means that we are using a two-tier reference (not a reference of a variable, but a reference of a data reference ). This is a bit confusing, but if the reference is completely new to you, I suggest you read this knowledge (although our goal is not to have to read C ). It will be helpful.

Another thing to understand about pointers is how they are applied in arrays of C (not arrays of PHP, but arrays in C ). Because the pointer is a memory address, we can define an array by allocating a piece of memory, and then traverse it by incrementing the pointer. Under normal circumstances, we can use a data type char that represents a character (8 bits) to store a character in a string. But we can also use it to access the byte following the string as we use an array. Therefore, we can store only one pointer in the first byte, instead of storing the same string in the variable. Then, we can increment the pointer (increase its memory address) to traverse the entire string.

Char * foo = "test"; // foo is a pointer to "t" to save "test" in memory fragment // to access "e ", we can use the following method: char e = foo [1]; char e = * (foo + 1); char e = * (++ foo );

Read the key variables and pointers in the C language and read this good free book.

Preprocessing description

C uses a step called "preprocessing" before compilation. This step involves optimizing and dynamically using part of the code based on the options you pass to the compiler. We will talk about two major preprocessors: conditional statements and macros.

Conditional statements allow code to be introduced when compiling output or not based on definitions. This looks like the following example. This allows different codes to be used according to different operating systems (so although they use different APIs, they can be used well in Windows and Linux ). In addition, it allows some code to be introduced or not based on definition instructions. In fact, this is how to compile PHP Execution in the configuration step.

#define FOO 1 #if FOO Foo is defined and not 0 #else Foo is not defined or is 0 #endif #ifdef FOO Foo is defined #else Foo is not defined #endif

Another example is that I call it a macro. This is the simplest mini function to simplify code. They are not real functions, but compilation preprocessing will execute simple text replacement. Therefore, macros do not actually call functions. You can write a macro for the function definition (in fact, PHP does this, but we will learn more about this in the following article ). What I want to say is that macros allow simpler code to be used during preprocessing and compilation.

#define FOO(a) ((a) + 1) int b = FOO(1); // Converted to int b = 1 + 1 Source File

In the last part, we need to understand two types of files used in C source code. There are two types of files:. c and. h .. The c file contains the source code to be compiled. Generally, the. c file contains the implementation of private functions that cannot be shared with other files .. H (or header file) defines functions that can be viewed by other files in the. c file, including preprocessing macros. The header file defines the public API by declaring the function signature without using the function weight (similar to interfaces and abstract methods in PHP ). In this way, the source code can use the header fileLinkTogether.

Next part

In the next part of this series, we will discuss how internal functions are defined in C. Therefore, you can jump to any internal function (suchStrlen) View its definition and how it works. Keep this pace.

Related Article

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.