Are you over 18 and want to see adult content?
More Annotations
![A complete backup of smartco-automation.org](https://www.archivebay.com/archive2/79703a11-fee6-491d-bb43-63d9b0578433.png)
A complete backup of smartco-automation.org
Are you over 18 and want to see adult content?
![A complete backup of theartreferences.tumblr.com](https://www.archivebay.com/archive2/fccc4cec-cee3-4139-aef0-b7253e3cec92.png)
A complete backup of theartreferences.tumblr.com
Are you over 18 and want to see adult content?
![A complete backup of meeplesgames.com](https://www.archivebay.com/archive2/b17d25bf-f786-49d5-95e2-f5d4e12132d5.png)
A complete backup of meeplesgames.com
Are you over 18 and want to see adult content?
![A complete backup of hesys-engineering.de](https://www.archivebay.com/archive2/aa9b4186-6921-4472-9e41-422eae9f1d6a.png)
A complete backup of hesys-engineering.de
Are you over 18 and want to see adult content?
Favourite Annotations
![A complete backup of motorcentralenumea.se](https://www.archivebay.com/archive2/bb8489bb-97e6-4e62-9ca5-2ca9699bfacb.png)
A complete backup of motorcentralenumea.se
Are you over 18 and want to see adult content?
![A complete backup of thecentsiblelife.com](https://www.archivebay.com/archive2/5dcf28ea-5ef6-455c-8020-aaee5c393afb.png)
A complete backup of thecentsiblelife.com
Are you over 18 and want to see adult content?
![A complete backup of pokerprolabs.com](https://www.archivebay.com/archive2/67bd04d1-1a19-417b-bbc9-b46d07442dd4.png)
A complete backup of pokerprolabs.com
Are you over 18 and want to see adult content?
![A complete backup of xn--80addfba7artbte.xn--p1ai](https://www.archivebay.com/archive2/74d7d358-ecea-4262-9757-9a821e9909a0.png)
A complete backup of xn--80addfba7artbte.xn--p1ai
Are you over 18 and want to see adult content?
![A complete backup of pressischarta.com](https://www.archivebay.com/archive2/bf2ee875-dd6b-4578-9171-bb5d3b9bc0fd.png)
A complete backup of pressischarta.com
Are you over 18 and want to see adult content?
Text
TABLE OF CONTENTS
The repository for this book is available on GitHub. The PHP internals book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. REGISTERING AND USING PHP FUNCTIONS Registering and using PHP functions¶. The main goal of a PHP extension is to register new PHP functions for userland. PHP functions are complex to fully understand their mechanics that hook very deep into the Zend Engine, but fortunately we don’t need this knowledge for our chapter, as the PHP extension mechanism provides many ways to abstract a lot such a complexity.BASIC STRUCTURE
To those not familiar with the concept of unions: A union defines multiple members of different types, but only one of them can ever be used at a time. E.g. if the value.lval member was set, then you also need to look up the value using value.lval and not one of the other members (doing so would violate “strict aliasing” guarantees and lead to undefined behaviour). HOOKS PROVIDED BY PHP Hooks provided by PHP. ¶. PHP and the Zend Engine provide many different hooks for extensions that allow extension developers to control the PHP runtime in ways that are not available from PHP userland. This chapter will show various hooks and common use-cases for hooking into them from an extension. The general pattern forhooking into PHP
BUILDING PHP EXTENSIONS Loading shared extensions¶. As you already know from the previous section, PHP extensions can be either built statically into the PHP binary, or compiled into a shared object (.so).Static linkage is the default for most of the bundled extensions, whereas shared objects can be created by explicitly passing --enable-EXTNAME=shared or --with-EXTNAME=shared to ./configure.MEMORY MANAGEMENT
Memory management¶. To work with zvals correctly and efficiently, it is important to understand how their memory management works. Broadly, we can classify values into two categories: Simple values like integers, which are stored directly inside the zval, and complex values like strings, for which the zval only stores a pointer to aseparate structure.
ZEND EXTENSIONS
The thing that differentiate them is the way they are loaded : PHP extensions (aka PHP “modules”) are loaded in INI files as a “extension=pib.so” line. Zend extensions are loaded in INI files as a “zend_extension=pib.so” line. That’s the only visible difference we see from PHP userland. But that’s a different storyfrom internal
MAGIC INTERFACES
When the interface is implemented the implement_comparable function will be called. In this function we override the classes create_object handler. To simplify things we only allow the interface to be used when create_object was NULL previously (i.e. it is a “normal” userland class). We could obviously also make this work with arbitrary classes by backing up the old create_object handlerDEBUGGING MEMORY
Debugging memory¶. This chapter is a quick introduction on memory debugging for PHP source code. It is not a full course: memory debugging is not hard, but you need some experience with it, by practicing a lot, what you’ll probably have to do anyway when designing any C-written code.ZEND MEMORY MANAGER
Zend Memory Manager¶. Zend Memory Manager, often abbreviated as ZendMM or ZMM, is a C layer that aims to provide abilities to allocate and release dynamic request-bound memory.. Note the “request-bound” in the above sentence. ZendMM is not just a classical layer over libc’s dynamic memory allocator, mainly represented by the couple API calls malloc()/free().TABLE OF CONTENTS
The repository for this book is available on GitHub. The PHP internals book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. REGISTERING AND USING PHP FUNCTIONS Registering and using PHP functions¶. The main goal of a PHP extension is to register new PHP functions for userland. PHP functions are complex to fully understand their mechanics that hook very deep into the Zend Engine, but fortunately we don’t need this knowledge for our chapter, as the PHP extension mechanism provides many ways to abstract a lot such a complexity.BASIC STRUCTURE
To those not familiar with the concept of unions: A union defines multiple members of different types, but only one of them can ever be used at a time. E.g. if the value.lval member was set, then you also need to look up the value using value.lval and not one of the other members (doing so would violate “strict aliasing” guarantees and lead to undefined behaviour). HOOKS PROVIDED BY PHP Hooks provided by PHP. ¶. PHP and the Zend Engine provide many different hooks for extensions that allow extension developers to control the PHP runtime in ways that are not available from PHP userland. This chapter will show various hooks and common use-cases for hooking into them from an extension. The general pattern forhooking into PHP
BUILDING PHP EXTENSIONS Loading shared extensions¶. As you already know from the previous section, PHP extensions can be either built statically into the PHP binary, or compiled into a shared object (.so).Static linkage is the default for most of the bundled extensions, whereas shared objects can be created by explicitly passing --enable-EXTNAME=shared or --with-EXTNAME=shared to ./configure.MEMORY MANAGEMENT
Memory management¶. To work with zvals correctly and efficiently, it is important to understand how their memory management works. Broadly, we can classify values into two categories: Simple values like integers, which are stored directly inside the zval, and complex values like strings, for which the zval only stores a pointer to aseparate structure.
ZEND EXTENSIONS
The thing that differentiate them is the way they are loaded : PHP extensions (aka PHP “modules”) are loaded in INI files as a “extension=pib.so” line. Zend extensions are loaded in INI files as a “zend_extension=pib.so” line. That’s the only visible difference we see from PHP userland. But that’s a different storyfrom internal
MAGIC INTERFACES
When the interface is implemented the implement_comparable function will be called. In this function we override the classes create_object handler. To simplify things we only allow the interface to be used when create_object was NULL previously (i.e. it is a “normal” userland class). We could obviously also make this work with arbitrary classes by backing up the old create_object handlerDEBUGGING MEMORY
Debugging memory¶. This chapter is a quick introduction on memory debugging for PHP source code. It is not a full course: memory debugging is not hard, but you need some experience with it, by practicing a lot, what you’ll probably have to do anyway when designing any C-written code.ZEND MEMORY MANAGER
Zend Memory Manager¶. Zend Memory Manager, often abbreviated as ZendMM or ZMM, is a C layer that aims to provide abilities to allocate and release dynamic request-bound memory.. Note the “request-bound” in the above sentence. ZendMM is not just a classical layer over libc’s dynamic memory allocator, mainly represented by the couple API calls malloc()/free().INTRODUCTION
Introduction ¶. This book is a collaborative effort between several developers of the PHP language to better document and describe how PHP works internally. There are three primary goals of this book: Document and describe how PHP internals work. Document andBUILDING PHP
The ./buildconf script¶. If you are building from the git repository, the first thing you’ll have to do is run the ./buildconf script. This script does little more than invoking the build/build.mk makefile, which in turn calls build/build2.mk.. The main job of these makefiles is to run autoconf to generate the ./configure script and autoheader to generate the main/php_config.h.in template.MEMORY MANAGEMENT
Memory management¶. To work with zvals correctly and efficiently, it is important to understand how their memory management works. Broadly, we can classify values into two categories: Simple values like integers, which are stored directly inside the zval, and complex values like strings, for which the zval only stores a pointer to aseparate structure.
BUILDING PHP
The ./buildconf script¶. If you are building from the git repository, the first thing you’ll have to do is run the ./buildconf script. This script does little more than invoking the build/build.mk makefile, which in turn calls build/build2.mk.. The main job of these makefiles is to run autoconf to generate the ./configure script and autoheader to generate the main/php_config.h.in template. ZVALS — PHP INTERNALS BOOK Zvals ¶. Zvals. ¶. In this chapter the zval data structure, which is used to represent arbitrary PHP values, is introduced. We explain the concepts behind zvals and how to use them in extension code. Contents: Basic structure. Types and values. The zval struct.EXTENSIONS DESIGN
Extensions design¶. In this chapter you’ll learn how to design PHP extensions. You’ll learn about the PHP lifecycle, how and when to manage memory, the different hooks you can use or the different function pointers you can replace to actively change PHP’s internalmachinery.
ZEND ENGINE
Zend engine¶. The Zend engine is a set of components that make PHP what it is. The most important Zend engine component is the Zend Virtual Machine, which is composed of the Zend Compiler and the Zend Executor components. We could also add the OPCache zend extension insuch category.
OBJECT HANDLERS
Object handlers¶. In the previous sections you already had some contact with object handlers. In particular you should know how to create the structure used to specify the handlers and how to implement cloning behavior using clone_obj.But this is just the beginning: Nearly all operations on objects in PHP go through object handlers and every magic method or magic interface is implemented withMEMORY MANAGEMENT
Memory management¶. C programmers usually have to deal with memory by hand. With dynamic memory, the programmer allocates memory when needing and frees it when finished.DEBUGGING MEMORY
Debugging memory¶. This chapter is a quick introduction on memory debugging for PHP source code. It is not a full course: memory debugging is not hard, but you need some experience with it, by practicing a lot, what you’ll probably have to do anyway when designing any C-written code.TABLE OF CONTENTS
The repository for this book is available on GitHub. The PHP internals book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. REGISTERING AND USING PHP FUNCTIONS Registering and using PHP functions¶. The main goal of a PHP extension is to register new PHP functions for userland. PHP functions are complex to fully understand their mechanics that hook very deep into the Zend Engine, but fortunately we don’t need this knowledge for our chapter, as the PHP extension mechanism provides many ways to abstract a lot such a complexity. BUILDING PHP EXTENSIONS Loading shared extensions¶. As you already know from the previous section, PHP extensions can be either built statically into the PHP binary, or compiled into a shared object (.so).Static linkage is the default for most of the bundled extensions, whereas shared objects can be created by explicitly passing --enable-EXTNAME=shared or --with-EXTNAME=shared to ./configure.EXTENSIONS DESIGN
Extensions design¶. In this chapter you’ll learn how to design PHP extensions. You’ll learn about the PHP lifecycle, how and when to manage memory, the different hooks you can use or the different function pointers you can replace to actively change PHP’s internalmachinery.
ZEND ENGINE
Zend engine¶. The Zend engine is a set of components that make PHP what it is. The most important Zend engine component is the Zend Virtual Machine, which is composed of the Zend Compiler and the Zend Executor components. We could also add the OPCache zend extension insuch category.
HOOKS PROVIDED BY PHP There are two extensions in PHP core that implement this hook: dtrace and opcache. If you start the PHP script with the environment variable USE_ZEND_DTRACE and compiled PHP with dtrace support, then dtrace_compile_file is used from Zend/zend_dtrace.c.. Opcache stores op arrays in shared memory for better performance, so that whenever a script is compiled its final op array is served from aDEBUGGING MEMORY
Debugging memory¶. This chapter is a quick introduction on memory debugging for PHP source code. It is not a full course: memory debugging is not hard, but you need some experience with it, by practicing a lot, what you’ll probably have to do anyway when designing any C-written code.OBJECT HANDLERS
Object handlers¶. In the previous sections you already had some contact with object handlers. In particular you should know how to create the structure used to specify the handlers and how to implement cloning behavior using clone_obj.But this is just the beginning: Nearly all operations on objects in PHP go through object handlers and every magic method or magic interface is implemented withMAGIC INTERFACES
When the interface is implemented the implement_comparable function will be called. In this function we override the classes create_object handler. To simplify things we only allow the interface to be used when create_object was NULL previously (i.e. it is a “normal” userland class). We could obviously also make this work with arbitrary classes by backing up the old create_object handlerTHE ZEND_STRING API
The two most important ones are ZSTR_VAL(), which returns the string contents as char *, and ZSTR_LEN(), which returns the string length as size_t.. The naming of these macros is slightly unfortunate in that both ZSTR_VAL / ZSTR_LEN, as well as Z_STRVAL / Z_STRLEN exist, and both only differ by the position of the underscore. Remember that ZSTR_* macros work on zend_string, while Z_ macrosTABLE OF CONTENTS
The repository for this book is available on GitHub. The PHP internals book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. REGISTERING AND USING PHP FUNCTIONS Registering and using PHP functions¶. The main goal of a PHP extension is to register new PHP functions for userland. PHP functions are complex to fully understand their mechanics that hook very deep into the Zend Engine, but fortunately we don’t need this knowledge for our chapter, as the PHP extension mechanism provides many ways to abstract a lot such a complexity. BUILDING PHP EXTENSIONS Loading shared extensions¶. As you already know from the previous section, PHP extensions can be either built statically into the PHP binary, or compiled into a shared object (.so).Static linkage is the default for most of the bundled extensions, whereas shared objects can be created by explicitly passing --enable-EXTNAME=shared or --with-EXTNAME=shared to ./configure.EXTENSIONS DESIGN
Extensions design¶. In this chapter you’ll learn how to design PHP extensions. You’ll learn about the PHP lifecycle, how and when to manage memory, the different hooks you can use or the different function pointers you can replace to actively change PHP’s internalmachinery.
ZEND ENGINE
Zend engine¶. The Zend engine is a set of components that make PHP what it is. The most important Zend engine component is the Zend Virtual Machine, which is composed of the Zend Compiler and the Zend Executor components. We could also add the OPCache zend extension insuch category.
HOOKS PROVIDED BY PHP There are two extensions in PHP core that implement this hook: dtrace and opcache. If you start the PHP script with the environment variable USE_ZEND_DTRACE and compiled PHP with dtrace support, then dtrace_compile_file is used from Zend/zend_dtrace.c.. Opcache stores op arrays in shared memory for better performance, so that whenever a script is compiled its final op array is served from aDEBUGGING MEMORY
Debugging memory¶. This chapter is a quick introduction on memory debugging for PHP source code. It is not a full course: memory debugging is not hard, but you need some experience with it, by practicing a lot, what you’ll probably have to do anyway when designing any C-written code.OBJECT HANDLERS
Object handlers¶. In the previous sections you already had some contact with object handlers. In particular you should know how to create the structure used to specify the handlers and how to implement cloning behavior using clone_obj.But this is just the beginning: Nearly all operations on objects in PHP go through object handlers and every magic method or magic interface is implemented withMAGIC INTERFACES
When the interface is implemented the implement_comparable function will be called. In this function we override the classes create_object handler. To simplify things we only allow the interface to be used when create_object was NULL previously (i.e. it is a “normal” userland class). We could obviously also make this work with arbitrary classes by backing up the old create_object handlerTHE ZEND_STRING API
The two most important ones are ZSTR_VAL(), which returns the string contents as char *, and ZSTR_LEN(), which returns the string length as size_t.. The naming of these macros is slightly unfortunate in that both ZSTR_VAL / ZSTR_LEN, as well as Z_STRVAL / Z_STRLEN exist, and both only differ by the position of the underscore. Remember that ZSTR_* macros work on zend_string, while Z_ macrosINTRODUCTION
Introduction¶. This book is a collaborative effort between several developers of the PHP language to better document and describe how PHPworks internally.
BUILDING PHP
The ./buildconf script¶. If you are building from the git repository, the first thing you’ll have to do is run the ./buildconf script. This script does little more than invoking the build/build.mk makefile, which in turn calls build/build2.mk.. The main job of these makefiles is to run autoconf to generate the ./configure script and autoheader to generate the main/php_config.h.in template. REFERENCES — PHP INTERNALS BOOK References¶. PHP references (in the sense of the & symbol) are mostly transparent to userland code, but require consistent special handling in the implementation. This chapter discusses how references are represented, and how internal code should deal with them.BASIC STRUCTURE
To those not familiar with the concept of unions: A union defines multiple members of different types, but only one of them can ever be used at a time. E.g. if the value.lval member was set, then you also need to look up the value using value.lval and not one of the other members (doing so would violate “strict aliasing” guarantees and lead to undefined behaviour).EXTENSIONS DESIGN
Extensions design¶. In this chapter you’ll learn how to design PHP extensions. You’ll learn about the PHP lifecycle, how and when to manage memory, the different hooks you can use or the different function pointers you can replace to actively change PHP’s internalmachinery.
MEMORY MANAGEMENT
Memory management¶. To work with zvals correctly and efficiently, it is important to understand how their memory management works. Broadly, we can classify values into two categories: Simple values like integers, which are stored directly inside the zval, and complex values like strings, for which the zval only stores a pointer to aseparate structure.
MANAGING GLOBAL STATE Like you can see, this extension picks a random integer when a request starts, and then the user - through pib_guess() - can try to guess the number. Once guessed, the number resets. If the user wants to, he could also call pib_reset() to reset the number himself by hand.. That random number has been implemented as a true C global variable.While this is not a problem if PHP is used inZEND MEMORY MANAGER
Zend Memory Manager¶. Zend Memory Manager, often abbreviated as ZendMM or ZMM, is a C layer that aims to provide abilities to allocate and release dynamic request-bound memory.. Note the “request-bound” in the above sentence. ZendMM is not just a classical layer over libc’s dynamic memory allocator, mainly represented by the couple API calls malloc()/free(). THE RESOURCE TYPE: ZEND_RESOURCE We find the traditional zend_refcounted_h header, meaning that resources are reference countable.. The handle is an integer that is used internally by the engine to locate the resource into an internal resource table. It is used as the key for such a table. The type is used to regroup resources of the same type together. This is about the way resources get destroyed and how they are fetched DECLARING AND USING INI SETTINGS The default value we are talking about here is called the “master value”.You may recall it from a phpinfo() output, right ?. The master value cannot change. If during a request, the user wants to change the configuration, f.e using ini_set(), and if he’s allowed to, then the changed value will be the “local value”, that is the current value for the current request.TABLE OF CONTENTS
The repository for this book is available on GitHub. The PHP internals book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. REGISTERING AND USING PHP FUNCTIONS Registering and using PHP functions¶. The main goal of a PHP extension is to register new PHP functions for userland. PHP functions are complex to fully understand their mechanics that hook very deep into the Zend Engine, but fortunately we don’t need this knowledge for our chapter, as the PHP extension mechanism provides many ways to abstract a lot such a complexity. BUILDING PHP EXTENSIONS Loading shared extensions¶. As you already know from the previous section, PHP extensions can be either built statically into the PHP binary, or compiled into a shared object (.so).Static linkage is the default for most of the bundled extensions, whereas shared objects can be created by explicitly passing --enable-EXTNAME=shared or --with-EXTNAME=shared to ./configure. HOOKS PROVIDED BY PHP There are two extensions in PHP core that implement this hook: dtrace and opcache. If you start the PHP script with the environment variable USE_ZEND_DTRACE and compiled PHP with dtrace support, then dtrace_compile_file is used from Zend/zend_dtrace.c.. Opcache stores op arrays in shared memory for better performance, so that whenever a script is compiled its final op array is served from aMEMORY MANAGEMENT
Memory management¶. To work with zvals correctly and efficiently, it is important to understand how their memory management works. Broadly, we can classify values into two categories: Simple values like integers, which are stored directly inside the zval, and complex values like strings, for which the zval only stores a pointer to aseparate structure.
OBJECT HANDLERS
Object handlers¶. In the previous sections you already had some contact with object handlers. In particular you should know how to create the structure used to specify the handlers and how to implement cloning behavior using clone_obj.But this is just the beginning: Nearly all operations on objects in PHP go through object handlers and every magic method or magic interface is implemented with LEARNING THE PHP LIFECYCLE Request initialization: RINIT()¶ A request just showed in, and PHP is about to treat it here. In RINIT(), you bootstrap the resources you need to treat that precise request.PHP is a share-nothing architecture, and as-is, it provides memory management facilities. In RINIT(), if you need to allocate dynamic memory, you’ll use Zend Memory Manager.You will call for emalloc().DEBUGGING MEMORY
Debugging memory¶. This chapter is a quick introduction on memory debugging for PHP source code. It is not a full course: memory debugging is not hard, but you need some experience with it, by practicing a lot, what you’ll probably have to do anyway when designing any C-written code.ZEND EXTENSIONS
On differences between PHP and Zend extensions¶. Just to say. Into PHP’s source code, PHP extensions are named as “PHP modules”, whereas Zend extensions are called “Zend extensions”.. So into PHP’s heart, if you read the “extension” keyword, you should first think about a Zend extension.MAGIC INTERFACES
When the interface is implemented the implement_comparable function will be called. In this function we override the classes create_object handler. To simplify things we only allow the interface to be used when create_object was NULL previously (i.e. it is a “normal” userland class). We could obviously also make this work with arbitrary classes by backing up the old create_object handlerTABLE OF CONTENTS
The repository for this book is available on GitHub. The PHP internals book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. REGISTERING AND USING PHP FUNCTIONS Registering and using PHP functions¶. The main goal of a PHP extension is to register new PHP functions for userland. PHP functions are complex to fully understand their mechanics that hook very deep into the Zend Engine, but fortunately we don’t need this knowledge for our chapter, as the PHP extension mechanism provides many ways to abstract a lot such a complexity. BUILDING PHP EXTENSIONS Loading shared extensions¶. As you already know from the previous section, PHP extensions can be either built statically into the PHP binary, or compiled into a shared object (.so).Static linkage is the default for most of the bundled extensions, whereas shared objects can be created by explicitly passing --enable-EXTNAME=shared or --with-EXTNAME=shared to ./configure. HOOKS PROVIDED BY PHP There are two extensions in PHP core that implement this hook: dtrace and opcache. If you start the PHP script with the environment variable USE_ZEND_DTRACE and compiled PHP with dtrace support, then dtrace_compile_file is used from Zend/zend_dtrace.c.. Opcache stores op arrays in shared memory for better performance, so that whenever a script is compiled its final op array is served from aMEMORY MANAGEMENT
Memory management¶. To work with zvals correctly and efficiently, it is important to understand how their memory management works. Broadly, we can classify values into two categories: Simple values like integers, which are stored directly inside the zval, and complex values like strings, for which the zval only stores a pointer to aseparate structure.
OBJECT HANDLERS
Object handlers¶. In the previous sections you already had some contact with object handlers. In particular you should know how to create the structure used to specify the handlers and how to implement cloning behavior using clone_obj.But this is just the beginning: Nearly all operations on objects in PHP go through object handlers and every magic method or magic interface is implemented with LEARNING THE PHP LIFECYCLE Request initialization: RINIT()¶ A request just showed in, and PHP is about to treat it here. In RINIT(), you bootstrap the resources you need to treat that precise request.PHP is a share-nothing architecture, and as-is, it provides memory management facilities. In RINIT(), if you need to allocate dynamic memory, you’ll use Zend Memory Manager.You will call for emalloc().DEBUGGING MEMORY
Debugging memory¶. This chapter is a quick introduction on memory debugging for PHP source code. It is not a full course: memory debugging is not hard, but you need some experience with it, by practicing a lot, what you’ll probably have to do anyway when designing any C-written code.ZEND EXTENSIONS
On differences between PHP and Zend extensions¶. Just to say. Into PHP’s source code, PHP extensions are named as “PHP modules”, whereas Zend extensions are called “Zend extensions”.. So into PHP’s heart, if you read the “extension” keyword, you should first think about a Zend extension.MAGIC INTERFACES
When the interface is implemented the implement_comparable function will be called. In this function we override the classes create_object handler. To simplify things we only allow the interface to be used when create_object was NULL previously (i.e. it is a “normal” userland class). We could obviously also make this work with arbitrary classes by backing up the old create_object handlerBASIC STRUCTURE
To those not familiar with the concept of unions: A union defines multiple members of different types, but only one of them can ever be used at a time. E.g. if the value.lval member was set, then you also need to look up the value using value.lval and not one of the other members (doing so would violate “strict aliasing” guarantees and lead to undefined behaviour). REFERENCES — PHP INTERNALS BOOK References¶. PHP references (in the sense of the & symbol) are mostly transparent to userland code, but require consistent special handling in the implementation. This chapter discusses how references are represented, and how internal code should deal with them.BUILDING PHP
The ./buildconf script¶. If you are building from the git repository, the first thing you’ll have to do is run the ./buildconf script. This script does little more than invoking the build/build.mk makefile, which in turn calls build/build2.mk.. The main job of these makefiles is to run autoconf to generate the ./configure script and autoheader to generate the main/php_config.h.in template. LEARNING THE PHP LIFECYCLE Request initialization: RINIT()¶ A request just showed in, and PHP is about to treat it here. In RINIT(), you bootstrap the resources you need to treat that precise request.PHP is a share-nothing architecture, and as-is, it provides memory management facilities. In RINIT(), if you need to allocate dynamic memory, you’ll use Zend Memory Manager.You will call for emalloc().MEMORY MANAGEMENT
Memory management¶. To work with zvals correctly and efficiently, it is important to understand how their memory management works. Broadly, we can classify values into two categories: Simple values like integers, which are stored directly inside the zval, and complex values like strings, for which the zval only stores a pointer to aseparate structure.
MANAGING GLOBAL STATE Like you can see, this extension picks a random integer when a request starts, and then the user - through pib_guess() - can try to guess the number. Once guessed, the number resets. If the user wants to, he could also call pib_reset() to reset the number himself by hand.. That random number has been implemented as a true C global variable.While this is not a problem if PHP is used in THE RESOURCE TYPE: ZEND_RESOURCE We find the traditional zend_refcounted_h header, meaning that resources are reference countable.. The handle is an integer that is used internally by the engine to locate the resource into an internal resource table. It is used as the key for such a table. The type is used to regroup resources of the same type together. This is about the way resources get destroyed and how they are fetched ZVALS — PHP INTERNALS BOOK Zvals¶. In this chapter the zval data structure, which is used to represent arbitrary PHP values, is introduced. We explain the concepts behind zvals and how to use them in extension code. Contents:MEMORY MANAGEMENT
Memory management¶. The zval structure has two roles: The first, which was described in the previous section, is to store a value and its type. The second, which will be covered in this section, is to efficiently manage those values in memory. SYMTABLE AND ARRAY API Symtables¶. One of the core concepts behind the design of PHP is that integers and strings containing integers should be interchangeable. This also applies to arrays where the keys 42 and "42" should be considered the same. This is not the case though with ordinary hashtables: They strictly distinguish the key types and it’s okay to have both the key 42 and "42" in the same table (withTABLE OF CONTENTS
The repository for this book is available on GitHub. The PHP internals book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. REGISTERING AND USING PHP FUNCTIONS Registering and using PHP functions¶. The main goal of a PHP extension is to register new PHP functions for userland. PHP functions are complex to fully understand their mechanics that hook very deep into the Zend Engine, but fortunately we don’t need this knowledge for our chapter, as the PHP extension mechanism provides many ways to abstract a lot such a complexity.BASIC STRUCTURE
To those not familiar with the concept of unions: A union defines multiple members of different types, but only one of them can ever be used at a time. E.g. if the value.lval member was set, then you also need to look up the value using value.lval and not one of the other members (doing so would violate “strict aliasing” guarantees and lead to undefined behaviour). BUILDING PHP EXTENSIONS Loading shared extensions¶. As you already know from the previous section, PHP extensions can be either built statically into the PHP binary, or compiled into a shared object (.so).Static linkage is the default for most of the bundled extensions, whereas shared objects can be created by explicitly passing --enable-EXTNAME=shared or --with-EXTNAME=shared to ./configure. HOOKS PROVIDED BY PHP Hooks provided by PHP. ¶. PHP and the Zend Engine provide many different hooks for extensions that allow extension developers to control the PHP runtime in ways that are not available from PHP userland. This chapter will show various hooks and common use-cases for hooking into them from an extension. The general pattern forhooking into PHP
MEMORY MANAGEMENT
Memory management¶. To work with zvals correctly and efficiently, it is important to understand how their memory management works. Broadly, we can classify values into two categories: Simple values like integers, which are stored directly inside the zval, and complex values like strings, for which the zval only stores a pointer to aseparate structure.
ZEND EXTENSIONS
The thing that differentiate them is the way they are loaded : PHP extensions (aka PHP “modules”) are loaded in INI files as a “extension=pib.so” line. Zend extensions are loaded in INI files as a “zend_extension=pib.so” line. That’s the only visible difference we see from PHP userland. But that’s a different storyfrom internal
OBJECT HANDLERS
Object handlers¶. In the previous sections you already had some contact with object handlers. In particular you should know how to create the structure used to specify the handlers and how to implement cloning behavior using clone_obj.But this is just the beginning: Nearly all operations on objects in PHP go through object handlers and every magic method or magic interface is implemented withDEBUGGING MEMORY
Debugging memory¶. This chapter is a quick introduction on memory debugging for PHP source code. It is not a full course: memory debugging is not hard, but you need some experience with it, by practicing a lot, what you’ll probably have to do anyway when designing any C-written code.MAGIC INTERFACES
When the interface is implemented the implement_comparable function will be called. In this function we override the classes create_object handler. To simplify things we only allow the interface to be used when create_object was NULL previously (i.e. it is a “normal” userland class). We could obviously also make this work with arbitrary classes by backing up the old create_object handlerTABLE OF CONTENTS
The repository for this book is available on GitHub. The PHP internals book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. REGISTERING AND USING PHP FUNCTIONS Registering and using PHP functions¶. The main goal of a PHP extension is to register new PHP functions for userland. PHP functions are complex to fully understand their mechanics that hook very deep into the Zend Engine, but fortunately we don’t need this knowledge for our chapter, as the PHP extension mechanism provides many ways to abstract a lot such a complexity.BASIC STRUCTURE
To those not familiar with the concept of unions: A union defines multiple members of different types, but only one of them can ever be used at a time. E.g. if the value.lval member was set, then you also need to look up the value using value.lval and not one of the other members (doing so would violate “strict aliasing” guarantees and lead to undefined behaviour). BUILDING PHP EXTENSIONS Loading shared extensions¶. As you already know from the previous section, PHP extensions can be either built statically into the PHP binary, or compiled into a shared object (.so).Static linkage is the default for most of the bundled extensions, whereas shared objects can be created by explicitly passing --enable-EXTNAME=shared or --with-EXTNAME=shared to ./configure. HOOKS PROVIDED BY PHP Hooks provided by PHP. ¶. PHP and the Zend Engine provide many different hooks for extensions that allow extension developers to control the PHP runtime in ways that are not available from PHP userland. This chapter will show various hooks and common use-cases for hooking into them from an extension. The general pattern forhooking into PHP
MEMORY MANAGEMENT
Memory management¶. To work with zvals correctly and efficiently, it is important to understand how their memory management works. Broadly, we can classify values into two categories: Simple values like integers, which are stored directly inside the zval, and complex values like strings, for which the zval only stores a pointer to aseparate structure.
ZEND EXTENSIONS
The thing that differentiate them is the way they are loaded : PHP extensions (aka PHP “modules”) are loaded in INI files as a “extension=pib.so” line. Zend extensions are loaded in INI files as a “zend_extension=pib.so” line. That’s the only visible difference we see from PHP userland. But that’s a different storyfrom internal
OBJECT HANDLERS
Object handlers¶. In the previous sections you already had some contact with object handlers. In particular you should know how to create the structure used to specify the handlers and how to implement cloning behavior using clone_obj.But this is just the beginning: Nearly all operations on objects in PHP go through object handlers and every magic method or magic interface is implemented withDEBUGGING MEMORY
Debugging memory¶. This chapter is a quick introduction on memory debugging for PHP source code. It is not a full course: memory debugging is not hard, but you need some experience with it, by practicing a lot, what you’ll probably have to do anyway when designing any C-written code.MAGIC INTERFACES
When the interface is implemented the implement_comparable function will be called. In this function we override the classes create_object handler. To simplify things we only allow the interface to be used when create_object was NULL previously (i.e. it is a “normal” userland class). We could obviously also make this work with arbitrary classes by backing up the old create_object handlerBASIC STRUCTURE
To those not familiar with the concept of unions: A union defines multiple members of different types, but only one of them can ever be used at a time. E.g. if the value.lval member was set, then you also need to look up the value using value.lval and not one of the other members (doing so would violate “strict aliasing” guarantees and lead to undefined behaviour).BUILDING PHP
The ./buildconf script¶. If you are building from the git repository, the first thing you’ll have to do is run the ./buildconf script. This script does little more than invoking the build/build.mk makefile, which in turn calls build/build2.mk.. The main job of these makefiles is to run autoconf to generate the ./configure script and autoheader to generate the main/php_config.h.in template. REFERENCES — PHP INTERNALS BOOK References¶. PHP references (in the sense of the & symbol) are mostly transparent to userland code, but require consistent special handling in the implementation. This chapter discusses how references are represented, and how internal code should deal with them.BUILDING PHP
The ./buildconf script¶. If you are building from the git repository, the first thing you’ll have to do is run the ./buildconf script. This script does little more than invoking the build/build.mk makefile, which in turn calls build/build2.mk.. The main job of these makefiles is to run autoconf to generate the ./configure script and autoheader to generate the main/php_config.h.in template. LEARNING THE PHP LIFECYCLE Request initialization: RINIT()¶ A request just showed in, and PHP is about to treat it here. In RINIT(), you bootstrap the resources you need to treat that precise request.PHP is a share-nothing architecture, and as-is, it provides memory management facilities. In RINIT(), if you need to allocate dynamic memory, you’ll use Zend Memory Manager.You will call for emalloc().MEMORY MANAGEMENT
Memory management¶. To work with zvals correctly and efficiently, it is important to understand how their memory management works. Broadly, we can classify values into two categories: Simple values like integers, which are stored directly inside the zval, and complex values like strings, for which the zval only stores a pointer to aseparate structure.
ZVALS — PHP INTERNALS BOOK Zvals ¶. Zvals. ¶. In this chapter the zval data structure, which is used to represent arbitrary PHP values, is introduced. We explain the concepts behind zvals and how to use them in extension code. Contents: Basic structure. Types and values. The zval struct.MEMORY MANAGEMENT
Memory management. ¶. The zval structure has two roles: The first, which was described in the previous section, is to store a value and its type. The second, which will be covered in this section, is to efficiently manage those values in memory. In the following we’ll have a look at the concepts of reference-counting and copy-on-writeand
MANAGING GLOBAL STATE Managing request globals ¶. Here is a simple example of a simple extension using a request global: Like you can see, this extension picks a random integer when a request starts, and then the user - through pib_guess () - can try to guess the number. Once guessed, the number resets. If the user wants to, he could also call pib_reset ()to reset
THE RESOURCE TYPE: ZEND_RESOURCE The Resource type: zend_resource¶. Even though PHP could really get rid of the “resource” type, because custom object storage allows to build a PHP representation of any abstract kind of data, that resource type still exists in the current version of PHP, and you may need to deal with it.TABLE OF CONTENTS
The repository for this book is available on GitHub. The PHP internals book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. REGISTERING AND USING PHP FUNCTIONS Registering and using PHP functions¶. The main goal of a PHP extension is to register new PHP functions for userland. PHP functions are complex to fully understand their mechanics that hook very deep into the Zend Engine, but fortunately we don’t need this knowledge for our chapter, as the PHP extension mechanism provides many ways to abstract a lot such a complexity.INTRODUCTION
Introduction ¶. This book is a collaborative effort between several developers of the PHP language to better document and describe how PHP works internally. There are three primary goals of this book: Document and describe how PHP internals work. Document and BUILDING PHP EXTENSIONS Loading shared extensions¶. As you already know from the previous section, PHP extensions can be either built statically into the PHP binary, or compiled into a shared object (.so).Static linkage is the default for most of the bundled extensions, whereas shared objects can be created by explicitly passing --enable-EXTNAME=shared or --with-EXTNAME=shared to ./configure.EXTENSIONS DESIGN
Extensions design¶. In this chapter you’ll learn how to design PHP extensions. You’ll learn about the PHP lifecycle, how and when to manage memory, the different hooks you can use or the different function pointers you can replace to actively change PHP’s internalmachinery.
HASHTABLES — PHP INTERNALS BOOK Hashtables ¶. Hashtables are one of the most important structures used by PHP. They form the basis for arrays, object properties, symbol tables and have countless other applications throughout the engine. This chapter will introduce how hashtables work and cover the related APIs in detail. Contents: HOOKS PROVIDED BY PHP Hooks provided by PHP. ¶. PHP and the Zend Engine provide many different hooks for extensions that allow extension developers to control the PHP runtime in ways that are not available from PHP userland. This chapter will show various hooks and common use-cases for hooking into them from an extension. The general pattern forhooking into PHP
MAGIC INTERFACES
When the interface is implemented the implement_comparable function will be called. In this function we override the classes create_object handler. To simplify things we only allow the interface to be used when create_object was NULL previously (i.e. it is a “normal” userland class). We could obviously also make this work with arbitrary classes by backing up the old create_object handlerDEBUGGING MEMORY
Debugging memory¶. This chapter is a quick introduction on memory debugging for PHP source code. It is not a full course: memory debugging is not hard, but you need some experience with it, by practicing a lot, what you’ll probably have to do anyway when designing any C-written code. A LOOK INTO A PHP EXTENSION AND EXTENSION SKELETON The skeleton only generated .c file. For the example, we called the extension “pib” so we got a pib.c file and we must uncomment the –enable-pib line in config.m4 for it to get compiled. Every C file comes with a header file (usually). Here, the structure is php_.h , sophp_pib.h for us.
TABLE OF CONTENTS
The repository for this book is available on GitHub. The PHP internals book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. REGISTERING AND USING PHP FUNCTIONS Registering and using PHP functions¶. The main goal of a PHP extension is to register new PHP functions for userland. PHP functions are complex to fully understand their mechanics that hook very deep into the Zend Engine, but fortunately we don’t need this knowledge for our chapter, as the PHP extension mechanism provides many ways to abstract a lot such a complexity.INTRODUCTION
Introduction ¶. This book is a collaborative effort between several developers of the PHP language to better document and describe how PHP works internally. There are three primary goals of this book: Document and describe how PHP internals work. Document and BUILDING PHP EXTENSIONS Loading shared extensions¶. As you already know from the previous section, PHP extensions can be either built statically into the PHP binary, or compiled into a shared object (.so).Static linkage is the default for most of the bundled extensions, whereas shared objects can be created by explicitly passing --enable-EXTNAME=shared or --with-EXTNAME=shared to ./configure.EXTENSIONS DESIGN
Extensions design¶. In this chapter you’ll learn how to design PHP extensions. You’ll learn about the PHP lifecycle, how and when to manage memory, the different hooks you can use or the different function pointers you can replace to actively change PHP’s internalmachinery.
HASHTABLES — PHP INTERNALS BOOK Hashtables ¶. Hashtables are one of the most important structures used by PHP. They form the basis for arrays, object properties, symbol tables and have countless other applications throughout the engine. This chapter will introduce how hashtables work and cover the related APIs in detail. Contents: HOOKS PROVIDED BY PHP Hooks provided by PHP. ¶. PHP and the Zend Engine provide many different hooks for extensions that allow extension developers to control the PHP runtime in ways that are not available from PHP userland. This chapter will show various hooks and common use-cases for hooking into them from an extension. The general pattern forhooking into PHP
MAGIC INTERFACES
When the interface is implemented the implement_comparable function will be called. In this function we override the classes create_object handler. To simplify things we only allow the interface to be used when create_object was NULL previously (i.e. it is a “normal” userland class). We could obviously also make this work with arbitrary classes by backing up the old create_object handlerDEBUGGING MEMORY
Debugging memory¶. This chapter is a quick introduction on memory debugging for PHP source code. It is not a full course: memory debugging is not hard, but you need some experience with it, by practicing a lot, what you’ll probably have to do anyway when designing any C-written code. A LOOK INTO A PHP EXTENSION AND EXTENSION SKELETON The skeleton only generated .c file. For the example, we called the extension “pib” so we got a pib.c file and we must uncomment the –enable-pib line in config.m4 for it to get compiled. Every C file comes with a header file (usually). Here, the structure is php_.h , sophp_pib.h for us.
INTRODUCTION
Introduction ¶. This book is a collaborative effort between several developers of the PHP language to better document and describe how PHP works internally. There are three primary goals of this book: Document and describe how PHP internals work. Document andEXTENSIONS DESIGN
Extensions design¶. In this chapter you’ll learn how to design PHP extensions. You’ll learn about the PHP lifecycle, how and when to manage memory, the different hooks you can use or the different function pointers you can replace to actively change PHP’s internalmachinery.
BUILDING PHP
The ./buildconf script¶. If you are building from the git repository, the first thing you’ll have to do is run the ./buildconf script. This script does little more than invoking the build/build.mk makefile, which in turn calls build/build2.mk.. The main job of these makefiles is to run autoconf to generate the ./configure script and autoheader to generate the main/php_config.h.in template. REFERENCES — PHP INTERNALS BOOK References¶. PHP references (in the sense of the & symbol) are mostly transparent to userland code, but require consistent special handling in the implementation. This chapter discusses how references are represented, and how internal code should deal with them.BASIC STRUCTURE
To those not familiar with the concept of unions: A union defines multiple members of different types, but only one of them can ever be used at a time. E.g. if the value.lval member was set, then you also need to look up the value using value.lval and not one of the other members (doing so would violate “strict aliasing” guarantees and lead to undefined behaviour). ZVALS — PHP INTERNALS BOOK Zvals ¶. Zvals. ¶. In this chapter the zval data structure, which is used to represent arbitrary PHP values, is introduced. We explain the concepts behind zvals and how to use them in extension code. Contents: Basic structure. Types and values. The zval struct. USING THE PHP BUILD SYSTEM Using the PHP build system¶. In this chapter we’ll explain how to use the PHP build system to compile both itself and additional extensions. This chapter will not yet be concerned with writing your own autoconf build instructions and only explain how to use thetooling.
MEMORY MANAGEMENT
Memory management¶. To work with zvals correctly and efficiently, it is important to understand how their memory management works. Broadly, we can classify values into two categories: Simple values like integers, which are stored directly inside the zval, and complex values like strings, for which the zval only stores a pointer to aseparate structure.
MEMORY MANAGEMENT
Memory management¶. C programmers usually have to deal with memory by hand. With dynamic memory, the programmer allocates memory when needing and frees it when finished. THE RESOURCE TYPE: ZEND_RESOURCE The Resource type: zend_resource¶. Even though PHP could really get rid of the “resource” type, because custom object storage allows to build a PHP representation of any abstract kind of data, that resource type still exists in the current version of PHP, and you may need to deal with it.TABLE OF CONTENTS
The repository for this book is available on GitHub. The PHP internals book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. REGISTERING AND USING PHP FUNCTIONS Registering and using PHP functions¶. The main goal of a PHP extension is to register new PHP functions for userland. PHP functions are complex to fully understand their mechanics that hook very deep into the Zend Engine, but fortunately we don’t need this knowledge for our chapter, as the PHP extension mechanism provides many ways to abstract a lot such a complexity.INTRODUCTION
Introduction¶. This book is a collaborative effort between several developers of the PHP language to better document and describe how PHPworks internally.
BUILDING PHP EXTENSIONS Loading shared extensions¶. As you already know from the previous section, PHP extensions can be either built statically into the PHP binary, or compiled into a shared object (.so).Static linkage is the default for most of the bundled extensions, whereas shared objects can be created by explicitly passing --enable-EXTNAME=shared or --with-EXTNAME=shared to ./configure.EXTENSIONS DESIGN
Extensions design¶. In this chapter you’ll learn how to design PHP extensions. You’ll learn about the PHP lifecycle, how and when to manage memory, the different hooks you can use or the different function pointers you can replace to actively change PHP’s internalmachinery.
HASHTABLES — PHP INTERNALS BOOK Hashtables¶. Hashtables are one of the most important structures used by PHP. They form the basis for arrays, object properties, symbol tables and have countless other applications throughout the engine.MAGIC INTERFACES
When the interface is implemented the implement_comparable function will be called. In this function we override the classes create_object handler. To simplify things we only allow the interface to be used when create_object was NULL previously (i.e. it is a “normal” userland class). We could obviously also make this work with arbitrary classes by backing up the old create_object handlerZEND MEMORY MANAGER
Zend Memory Manager¶. Zend Memory Manager, often abbreviated as ZendMM or ZMM, is a C layer that aims to provide abilities to allocate and release dynamic request-bound memory.. Note the “request-bound” in the above sentence. ZendMM is not just a classical layer over libc’s dynamic memory allocator, mainly represented by the couple API calls malloc()/free().DEBUGGING MEMORY
Debugging memory¶. This chapter is a quick introduction on memory debugging for PHP source code. It is not a full course: memory debugging is not hard, but you need some experience with it, by practicing a lot, what you’ll probably have to do anyway when designing any C-written code. A LOOK INTO A PHP EXTENSION AND EXTENSION SKELETON The four first parameters have already been explained in the building extensions chapter.They are usually filled-in using the STANDARD_MODULE_HEADER macro. The ini_entry vector is actually unused. You register INI entries using special macros.. Then you may declare dependencies, that means that your extension could need another extension to be loaded before it, or could declare a conflict withTABLE OF CONTENTS
The repository for this book is available on GitHub. The PHP internals book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. REGISTERING AND USING PHP FUNCTIONS Registering and using PHP functions¶. The main goal of a PHP extension is to register new PHP functions for userland. PHP functions are complex to fully understand their mechanics that hook very deep into the Zend Engine, but fortunately we don’t need this knowledge for our chapter, as the PHP extension mechanism provides many ways to abstract a lot such a complexity.INTRODUCTION
Introduction¶. This book is a collaborative effort between several developers of the PHP language to better document and describe how PHPworks internally.
BUILDING PHP EXTENSIONS Loading shared extensions¶. As you already know from the previous section, PHP extensions can be either built statically into the PHP binary, or compiled into a shared object (.so).Static linkage is the default for most of the bundled extensions, whereas shared objects can be created by explicitly passing --enable-EXTNAME=shared or --with-EXTNAME=shared to ./configure.EXTENSIONS DESIGN
Extensions design¶. In this chapter you’ll learn how to design PHP extensions. You’ll learn about the PHP lifecycle, how and when to manage memory, the different hooks you can use or the different function pointers you can replace to actively change PHP’s internalmachinery.
HASHTABLES — PHP INTERNALS BOOK Hashtables¶. Hashtables are one of the most important structures used by PHP. They form the basis for arrays, object properties, symbol tables and have countless other applications throughout the engine.MAGIC INTERFACES
When the interface is implemented the implement_comparable function will be called. In this function we override the classes create_object handler. To simplify things we only allow the interface to be used when create_object was NULL previously (i.e. it is a “normal” userland class). We could obviously also make this work with arbitrary classes by backing up the old create_object handlerZEND MEMORY MANAGER
Zend Memory Manager¶. Zend Memory Manager, often abbreviated as ZendMM or ZMM, is a C layer that aims to provide abilities to allocate and release dynamic request-bound memory.. Note the “request-bound” in the above sentence. ZendMM is not just a classical layer over libc’s dynamic memory allocator, mainly represented by the couple API calls malloc()/free().DEBUGGING MEMORY
Debugging memory¶. This chapter is a quick introduction on memory debugging for PHP source code. It is not a full course: memory debugging is not hard, but you need some experience with it, by practicing a lot, what you’ll probably have to do anyway when designing any C-written code. A LOOK INTO A PHP EXTENSION AND EXTENSION SKELETON The four first parameters have already been explained in the building extensions chapter.They are usually filled-in using the STANDARD_MODULE_HEADER macro. The ini_entry vector is actually unused. You register INI entries using special macros.. Then you may declare dependencies, that means that your extension could need another extension to be loaded before it, or could declare a conflict withINTRODUCTION
Introduction¶. This book is a collaborative effort between several developers of the PHP language to better document and describe how PHPworks internally.
EXTENSIONS DESIGN
Extensions design¶. In this chapter you’ll learn how to design PHP extensions. You’ll learn about the PHP lifecycle, how and when to manage memory, the different hooks you can use or the different function pointers you can replace to actively change PHP’s internalmachinery.
BUILDING PHP
The ./buildconf script¶. If you are building from the git repository, the first thing you’ll have to do is run the ./buildconf script. This script does little more than invoking the build/build.mk makefile, which in turn calls build/build2.mk.. The main job of these makefiles is to run autoconf to generate the ./configure script and autoheader to generate the main/php_config.h.in template.BASIC STRUCTURE
To those not familiar with the concept of unions: A union defines multiple members of different types, but only one of them can ever be used at a time. E.g. if the value.lval member was set, then you also need to look up the value using value.lval and not one of the other members (doing so would violate “strict aliasing” guarantees and lead to undefined behaviour). ZVALS — PHP INTERNALS BOOK The repository for this book is available on GitHub. The PHP internals book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. USING THE PHP BUILD SYSTEM Using the PHP build system¶. In this chapter we’ll explain how to use the PHP build system to compile both itself and additional extensions. This chapter will not yet be concerned with writing your own autoconf build instructions and only explain how to use thetooling.
MEMORY MANAGEMENT
Memory management¶. To work with zvals correctly and efficiently, it is important to understand how their memory management works. Broadly, we can classify values into two categories: Simple values like integers, which are stored directly inside the zval, and complex values like strings, for which the zval only stores a pointer to aseparate structure.
OBJECT HANDLERS
Object handlers¶. In the previous sections you already had some contact with object handlers. In particular you should know how to create the structure used to specify the handlers and how to implement cloning behavior using clone_obj.But this is just the beginning: Nearly all operations on objects in PHP go through object handlers and every magic method or magic interface is implemented withMEMORY MANAGEMENT
Memory management¶. C programmers usually have to deal with memory by hand. With dynamic memory, the programmer allocates memory when needing and frees it when finished. THE RESOURCE TYPE: ZEND_RESOURCE The Resource type: zend_resource¶. Even though PHP could really get rid of the “resource” type, because custom object storage allows to build a PHP representation of any abstract kind of data, that resource type still exists in the current version of PHP, and you may need to deal with it.PHP INTERNALS BOOK
TABLE OF CONTENTS
Contents :: Introduction »TABLE OF CONTENTS¶
PHP 7 AND PHP 8¶
* Introduction
* Using the PHP build system* Building PHP
* Why not use packages? * Obtaining the source code* Build overview
* The ./buildconf script * The ./configure script * make and make install * Running the test suite * Fixing compilation problems and make clean * Building PHP extensions * Loading shared extensions * Installing extensions from PECL * Adding extensions to the PHP source tree * Building extensions using phpize * Displaying information about extensions* Zvals
* Basic structure
* Types and values
* The zval struct
* Access macros
* Memory management
* Reference-counted values * Zval memory management* References
* Reference semantics* Representation
* Initializing references * Dereferencing and unwrapping* Indirect zvals
* Casts and operations* Casts
* Operations
* Internal types
* Strings
* The zend_string API* smart_str API
* PHP’s custom printf functions * The Resource type: zend_resource * What is the “Resource” type? * Resource types and resource destruction * Playing with resources * Reference counting resources * Persistent resources * HashTables: zend_array * Functions: zend_function * Objects and classes* Extensions design
* Learning the PHP lifecycle * The parallelism models * The PHP extensions hooks * Hooking by overwriting function pointers * A look into a PHP extension and extension skeleton * How the engine loads extensions * What is a PHP extension ? * Generating extension skeleton with scripts * New age of the extension skeleton generator* Publishing API
* Registering and using PHP functions * zend_function_entry structure * Registering PHP functions * Declaring function arguments * The PHP function structure and API, in C* Adding tests
* Playing with constants * A go with Hashtables (PHP arrays) * Managing references * Managing global state * Managing request globals * Using true globals * Publishing extension information* MINFO() hook
* A note about the Reflection API * Hooks provided by PHP * Hooking into the execution of functions * Overwriting an Internal Function * Modifying the Abstract Syntax Tree (AST) * Hooking into Script/File Compilation * Notification when Error Handler is called * Notification when Exception is thrown * Hooking into eval() * Hooking into the Garbage Collector * Overwrite Interrupt Handler * Replacing Opcode Handlers * Declaring and using INI settings * Reminders on INI settings * Zoom on INI settings * Registering and using INI entries * Validators and globals memory bridge* Using a displayer
* Zend Extensions
* On differences between PHP and Zend extensions * What is a Zend extension ? * Why need a Zend extension ? * API versions and conflicts management * Zend extensions lifetime hooks * Practice : my first example Zend extension* Hybrid extensions
* Memory management
* Zend Memory Manager * The two main kind of dynamic memory pools in PHP * Zend Memory Manager API * Zend Memory Manager debugging shields * ZendMM internal design * Common errors and mistakes* Debugging memory
* A quick note about valgrind* Before starting
* Memory leak detection example * Buffer overflow/underflow detection* Conclusions
* Zend engine
* Zend Compiler
* Zend Executor
* Zend OPCache
* Debugging with GDBPHP 5¶
* Introduction
* Using the PHP build system* Building PHP
* Building PHP extensions*
Creating PHP extensions* Zvals
* Basic structure
* Memory management
* Casts and operations*
Implementing functions* Hashtables
* Basic structure
* Hashtable API
* Symtable and array API * Hash algorithm and collisions * Classes and objects* Simple classes
* Custom object storage * Implementing typed arrays* Object handlers
* Iterators
* Serialization
* Magic interfaces - Comparable * Internal structures and implementation TESTING PHP SOURCE¶ Writing tests applies to both PHP 5 & PHP 7. * Testing with .phpt files* @TODO
* Testing overview
* Black-box testing
* Where the test files live * Running the test suite * Running tests directly with run-tests.php * Running tests via the Makefile (recommended) * Executing tests in parallel* More options
* The .phpt file structure * A basic test example * Some notable sections * Writing a simple test * Examining failed test output * Failed test output files * Examine failed test output inlineINDEX AND SEARCH¶
*
Index
*
Search Page
Contents :: Introduction » The repository for this book is available on GitHub.
The PHP internals book is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Details
Copyright © 2024 ArchiveBay.com. All rights reserved. Terms of Use | Privacy Policy | DMCA | 2021 | Feedback | Advertising | RSS 2.0