php internals

24
PHP Internals Nico Loubser Developer at …..

Upload: nico-loubser

Post on 23-Jan-2018

151 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: PHP Internals

PHP Internals

Nico LoubserDeveloper at …..

Page 2: PHP Internals
Page 3: PHP Internals

“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

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

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

Lexical analysis

Step one. Converts a script into tokens by Zend’s token engine. Really? Yes

really.

Page 7: PHP Internals

Lexical analysis. What does tokenized data look like?

Page 8: PHP Internals

Lexical analysis - can we use it for anything?

Source code highlighting and other tools.

Page 9: PHP Internals

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

AST - Generated code

Page 11: PHP Internals

AST - Generated code

Page 12: PHP Internals
Page 13: PHP Internals
Page 14: PHP Internals

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

Vulcan Logic Disassembler

Page 16: PHP Internals
Page 17: PHP Internals

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

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
Page 20: PHP Internals

Simulation of the copy-on-write behavior

Time to run some scripts…..

[Script can be found in github]

Page 21: PHP Internals

Extension

The hard way.

The PHP way.

The C++ way.

Page 22: PHP Internals

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

Comparison of normal vs extension

Page 24: PHP Internals

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.

[email protected]

@Nico_Loubser