26
PHP Internals

PHP Internals GDG

Embed Size (px)

Citation preview

Page 1: PHP Internals GDG

PHP Internals

Page 2: PHP Internals GDG

Why

Page 3: PHP Internals GDG

Dynamical Typed

* not the same

x = “1”;

$x = “1a”+1;

Weakly Typed

Page 4: PHP Internals GDG

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

Page 5: PHP Internals GDG

PHP Implementations• HHVM • PHP Compiler • Phalanger • PHP4Mono

Page 6: PHP Internals GDG

PHP has no Specification yet

Page 7: PHP Internals GDG

PHP Overview

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

Page 8: PHP Internals GDG

Cycle

SAPI

Starting Up

CLI / mod_php

Stopping

Request

Engine Start

Config Init

MINIT

Process Request

MSHUTDOWN

Engine Shutdown

Page 9: PHP Internals GDG

SAPI CLI

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

Page 10: PHP Internals GDG

SAPI Single Process

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

Page 11: PHP Internals GDG

SAPI Multiprocess (Apache 2, ISAPI)

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

Page 12: PHP Internals GDG

SAPI - Multithreaded

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

Page 13: PHP Internals GDG

PHP Life Cycle (1)

Page 14: PHP Internals GDG

Variables & copy on write

Page 15: PHP Internals GDG

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;

Page 16: PHP Internals GDG

Refcount

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

Page 17: PHP Internals GDG

is_ref

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

Page 18: PHP Internals GDG

Complie

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

Page 19: PHP Internals GDG

Lexical analysis

Page 20: PHP Internals GDG

Tokens

Page 21: PHP Internals GDG

Parser

Page 22: PHP Internals GDG

OpCodes

Page 23: PHP Internals GDG

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;

Page 24: PHP Internals GDG

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; }

Page 25: PHP Internals GDG

Singlish PHP ?

Page 26: PHP Internals GDG