php internals
TRANSCRIPT
![Page 1: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/1.jpg)
PHP Internals
Nico LoubserDeveloper at …..
![Page 2: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/2.jpg)
![Page 3: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/3.jpg)
“One night only” Curriculum.
Focus on concepts that will allow you to build things.
Lexical analysis
Syntax analysis [PHP7]
Opcode
Extensions
And some interesting facts about PHP
![Page 4: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/4.jpg)
Terminology
1. Zvals are the datastructures holding PHP variable data. Variable points to a
Zval
2. A Reference !== Passing by reference. Reference === `Points`
3. Variables references zvals
4. Heap is the memory where zvals live
![Page 5: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/5.jpg)
PHP Execution
Interpreted, compiled, or both?
These are the typical steps of a multipass compiler
- Lexical analysis
- Syntax analysis
- Some other steps maybe….
- Opcode [Final compilation]
![Page 6: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/6.jpg)
Lexical analysis
Step one. Converts a script into tokens by Zend’s token engine. Really? Yes
really.
![Page 7: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/7.jpg)
Lexical analysis. What does tokenized data look like?
![Page 8: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/8.jpg)
Lexical analysis - can we use it for anything?
Source code highlighting and other tools.
![Page 9: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/9.jpg)
Syntax analysis
PHP 7 introduced AST
More maintainable parser and compiler
Decoupling syntax decisions from technical issues
AST - parse tree for PHP script. [ https://github.com/nikic/PHP-Parser ]
![Page 10: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/10.jpg)
AST - Generated code
![Page 11: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/11.jpg)
AST - Generated code
![Page 12: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/12.jpg)
![Page 13: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/13.jpg)
![Page 14: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/14.jpg)
Opcode
VLD / Bytekit / Parsekit
I used VLD, easiest to get to work.
Vulcan Logic Disassembler
Bytekit doesn’t seem to be supported, Parsekit results same is VLD.
![Page 15: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/15.jpg)
Vulcan Logic Disassembler
![Page 16: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/16.jpg)
![Page 17: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/17.jpg)
ZVALS
How PHP represents data and keeps count of references for instance.
Represented by a C type called a Union.
PHP5
![Page 18: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/18.jpg)
Zvals
The important difference between PHP 5 and PHP 7 : share the same Zval, regardless of by value or
reference.
Only once some kind of modification is performed the array will be separated.
![Page 19: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/19.jpg)
![Page 20: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/20.jpg)
Simulation of the copy-on-write behavior
Time to run some scripts…..
[Script can be found in github]
![Page 21: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/21.jpg)
Extension
The hard way.
The PHP way.
The C++ way.
![Page 22: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/22.jpg)
Extensions
I used the PHP-CPP skeleton framework. http://www.php-cpp.com/
You will need sudo apt-get install php5-dev
In Makefile
Assign your extension name to the NAME parameter, and then create a ini file with the same name.
Put this in the ini file
extension=quintillion.so
Make && make install
Run php -i | grep quintillion
/etc/php5/cli/conf.d/quintillion.ini
![Page 23: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/23.jpg)
Comparison of normal vs extension
![Page 24: PHP Internals](https://reader034.vdocuments.net/reader034/viewer/2022050720/5883c7ee1a28abb7308b4b19/html5/thumbnails/24.jpg)
Live long and prosper
Internals is very interesting topic.
Knowledge about internals is good for memory, good for speed, data-structures, native implementations to interface with something.
@Nico_Loubser