PHP Internals GDG

Preview:

Citation preview

PHP Internals

Why

Dynamical Typed

* not the same

x = “1”;

$x = “1a”+1;

Weakly Typed

Variables & copy on write

<?php

$ch = curl_init(); $post_data = array('a' => 1);

var_dump($post_data); // int a curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); var_dump($post_data); // string a

Example 1

PHP Implementations• HHVM • PHP Compiler • Phalanger • PHP4Mono

PHP has no Specification yet

PHP Overview

http://img.my.csdn.net/uploads/201107/4/0_1309773216dp36.gif

Cycle

SAPI

Starting Up

CLI / mod_php

Stopping

Request

Engine Start

Config Init

MINIT

Process Request

MSHUTDOWN

Engine Shutdown

SAPI CLI

http://flylib.com/books/2/565/1/html/2/images/01fig01.jpg

SAPI Single Process

http://flylib.com/books/2/565/1/html/2/images/01fig02.jpg

SAPI Multiprocess (Apache 2, ISAPI)

http://flylib.com/books/2/565/1/html/2/images/01fig03.jpg

SAPI - Multithreaded

http://img.my.csdn.net/uploads/201107/4/0_1309773216dp36.gif

PHP Life Cycle (1)

Variables & copy on write

Zvals (1)typedef struct _zval_struct { zvalue_value value; /* variable value */ zend_uint refcount__gc; /* reference counter */ zend_uchar type; /* value type */ zend_uchar is_ref__gc; /* reference flag */} zval;

typedef union _zvalue_value { long lval; /* long value */ double dval; /* double value */ struct { char *val; int len; /* this will always be set for strings */ } str; /* string (always has length) */ HashTable *ht; /* an array */ zend_object_value obj; /* stores an object store handle, and handlers */} zvalue_value;

Refcount

http://www.phpinternalsbook.com/zvals/memory_management.html#reference-counting-and-copy-on-write

is_ref

http://www.phpinternalsbook.com/zvals/memory_management.html#reference-counting-and-copy-on-write

Complie

• lexical Anaysis- zend_language.scanner.i • Parser - zend_language_parser.y (yacc) • Zend VM -

Lexical analysis

Tokens

Parser

OpCodes

Increments - Pre

http://www.phpinternalsbook.com/zvals/memory_management.html#reference-counting-and-copy-on-write

What is faster ?

• ++$a • $a++ • $a += 1; • $a = $a +1;

Function call

http://www.phpinternalsbook.com/zvals/memory_management.html#reference-counting-and-copy-on-write

<?php

$b = "1";

hello($b);

function hello($a){ echo $a; }

<ST_IN_SCRIPTING>"while" { return T_WHILE; }

Singlish PHP ?