phalcon the fastest php framework 阿土伯

81
Phalcon PHP Framework The Fastest Faster PHP Framework 阿土伯

Upload: hashmanlin

Post on 16-Nov-2014

4.016 views

Category:

Documents


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Phalcon   the fastest php framework 阿土伯

Phalcon  PHP  Framework   The  Fastest  Faster  PHP  Framework

阿土伯

Page 2: Phalcon   the fastest php framework 阿土伯

about:me

!   目前為 ViViPOS  Co.,  Ltd  技術總監  !   利用 javascript  寫 〞收銀機〞(傳統產業,全新感受)  

! CoCo  都可、50嵐、歇腳亭、Yamaha  、星聚點  KTV    

!   #3  commiHer  of  cphalcon  repository  

!   PHP  /  Javascript  /  Java  Programmer  (目前移情於 Scala  )  

! [email protected]  

! hHps://twiHer.com/racklin  

! hHps://www.facebook.com/racklin1002  

! hHp://racklin.blogspot.tw/      (生小孩後就變癈墟)  

Page 3: Phalcon   the fastest php framework 阿土伯

議題

! PhalconPHP  Framework 介紹  

! PhalconPHP  Kernel 介紹  

!   DEMO  c9s  ValidaWonKit  porWng  as  php  extension  

Page 4: Phalcon   the fastest php framework 阿土伯

Programmers  共同的罩門  

!  速度最快!  

!  使用資源最小!  

!  跨平台!  

 

Page 5: Phalcon   the fastest php framework 阿土伯

Programmers  共同的罩門  

!  速度最快!  

!  使用資源最小!  

!  跨平台!  

 

  所以大家來聽 PhalconPHP  

Page 6: Phalcon   the fastest php framework 阿土伯

為什麼快

!   Fibonacci  40  (費式數列)  hHp://bit.ly/Pk8HkJ  

Language Times

C  with  –O2 0m0.202s

java 0m1.305s

scala 0m1.808s

nodejs 0m2.872s

Ruby  (1.9.x) 0m37.404s

Php  (5.4) 1m17.728s

Python  (2.7.2) 1m17.979s

Ruby  (1.8.x) 3m35.135s

Php  (5.3) 6m17.192s

Page 7: Phalcon   the fastest php framework 阿土伯

Fibonacci  40

0  

100  

200  

300  

400  

500  

c  with  -­‐O2  

java   scala   nodejs   ruby  1.9.2  

php  5.4   ruby  1.8.x  

php  5.3  

Page 8: Phalcon   the fastest php framework 阿土伯

所以結論…  

下一個專案要用 c  /  c++  來寫  web    

Page 9: Phalcon   the fastest php framework 阿土伯

Raphters !   A  web  framework  for  C

Page 10: Phalcon   the fastest php framework 阿土伯

WOW!  COOOOOOL~ 看起來好像很容易!

Page 11: Phalcon   the fastest php framework 阿土伯

除了慣C哥    

大部份人打完招呼不知道要做什麼…..

Page 12: Phalcon   the fastest php framework 阿土伯

PHP  沒救了?

Page 13: Phalcon   the fastest php framework 阿土伯

Fibonacci  as  php  extension

Page 14: Phalcon   the fastest php framework 阿土伯

Fibonacci  as  php  extension

0m0.79s        馬上變成第二名

Page 15: Phalcon   the fastest php framework 阿土伯

Fibonacci  圖表

0  50  

100  150  200  250  300  350  400  450  

c  with  -­‐O2   java   scala   nodejs  ruby  1.9.2  php  5.4  ruby  1.8.x  php  5.3  php  ext  

Page 16: Phalcon   the fastest php framework 阿土伯

速度只要出問題,  C  都能搞定

Page 17: Phalcon   the fastest php framework 阿土伯

漂亮的平衡  

A  PHP  web  framework    delivered  as  a  C  extension

Page 18: Phalcon   the fastest php framework 阿土伯

PHP  extension  frameworks  

PhalconPHP  Yaf

Page 19: Phalcon   the fastest php framework 阿土伯

為什麼不是 Yaf

!   Underscore  Class  Name  (  目標 PHP  5.2 和以上版本)  

!   Document 實在不多  

!   Sample  Codes    更少  

 

Page 20: Phalcon   the fastest php framework 阿土伯

為什麼不是 Yaf

!   Underscore  Class  Name  ( 目標 PHP  5.2 和以上版本)  

!   Document 實在不多  

!   Sample  Codes    更少  

 

!   Yet  Another  Framework  (Yaf)    這專案名稱我不喜歡  很怕將來會出一個  What  The  Framework  (WTx)  

Page 21: Phalcon   the fastest php framework 阿土伯

Frameworks  benchmark

Page 22: Phalcon   the fastest php framework 阿土伯

Frameworks  memory  usage

Page 23: Phalcon   the fastest php framework 阿土伯

PhalconPHP  benefits

!   PHP  Extension  –  Faster  

! Phalcon  all  Components  are  loosely  coupled  

!   Low-­‐Level  OpWmizaWons  

!   Using  c-­‐language  ORM  for  PHP  

!   PHQL  –  lemon  parser  (SQLite)  

!   Well-­‐documented  

Page 24: Phalcon   the fastest php framework 阿土伯

PhalconPHP  0.5  news

!   Namespaces  

!   Class  Loader  –  相容於 PSR-­‐0  

!   Dependency  Injec8on  

!   Events  Manager  

!   PDO  Database  Adapters  !   *  支援 SQLite.      !   *  PostgreSQL  支援 Mul8-­‐Schemas  

!   *  CLI  Applica8ons  

!   Micro  Applica8ons  

!   Full  stack  frameworks  

Page 25: Phalcon   the fastest php framework 阿土伯

PhalconPHP  0.6

!   Object-­‐Document  Mapper  -­‐  MongoDB  

!   Volt:  Template  Engine    -­‐  inspired  on  Twig  

!   PS.  11/2  Released  

Page 26: Phalcon   the fastest php framework 阿土伯

Class  Loader  Dependency  InjecWon  

Events  Manager      

Flexible  Extensible  Plugin-­‐able    

Page 27: Phalcon   the fastest php framework 阿土伯

Class  Loader

!   盡可能不用使用 include  /  require  .  

! registerNamespaces  –  PSR-­‐0  相容  

! registerPrefixes  

! registerClasses  

! registerDirectories  

Page 28: Phalcon   the fastest php framework 阿土伯

Using  Class  Loader

Page 29: Phalcon   the fastest php framework 阿土伯

Composer •  類似 PEAR  的套件管理工具,但更方便! 像是 npm  /  brew  。  •  Install  Composer  

•  IniWal  project  

•  Require  /  Install  libraries  

•  Update  libraries  

$  curl  -­‐s  hHps://getcomposer.org/installer  |  php

$  composer.phar  init

$  composer.phar  require  [vendor/name]

$  composer.phar  update

Page 30: Phalcon   the fastest php framework 阿土伯

Composer  Autoloading

•  只要加一行在你的程式中  require  'vendor/autoload.php';

hHps://packagist.org/

Page 31: Phalcon   the fastest php framework 阿土伯

Phalcon  Class  Loader  +    Composer  Autloading

! Phalcon  and  Composer  都是使用 spl_autoload_register  ,  spl_autoload_register 相當聰明,載入類別後會中斷執行,不會重覆載入  

!   註冊 多個 autoload  handlers  是安全的        

!   所以只要在 Phalcon  Class  Loader  前先加上一行即可  

 

require  'vendor/autoload.php’;  

Page 32: Phalcon   the fastest php framework 阿土伯

Dependency  InjecWon

!   \Phalcon\DI  實作了 IoC  設計模式,幫助寫出可擴充性、易於測試、維護的程式碼。  

! PhalconPHP  內部大量利用 DI  將各元件串接起來。  

 

Page 33: Phalcon   the fastest php framework 阿土伯

Dependency  InjecWon

!   產生新的物件或單例(Singleton) - get  /  getShared  

!   傳參數給  Constructor  

!   Lazy  loading  !   物件只有在被取得時才會產生  

!   Magic    !   如果沒有註冊,會自動試著找同樣的 Class  Name  

!   如果可以,全部利用 DI  來產生您的類別實例。

Page 34: Phalcon   the fastest php framework 阿土伯

DI  範例  (set)

Page 35: Phalcon   the fastest php framework 阿土伯

DI  範例  (get)

!   取得物件  $request  =  $di-­‐>get("request");  

!   透過 magic  method  $request  =  $di-­‐>getRequest();  

!   Singleton  $request  =  $di-­‐>getShared("request");  

!   傳參數給 constructor  $component  =  $di-­‐>get("MyComponent",  array("some-­‐parameter",  "other"))

Page 36: Phalcon   the fastest php framework 阿土伯

Events  Manager

!   Web  GUI  程式中,我們大量使用 Events 。  !   DOM  Events  

!   Events  Manager  -­‐  fire  event  ,  aHach  listener  

!   Event  Type  Using  colon  :  as  namespace  separator.    

Page 37: Phalcon   the fastest php framework 阿土伯

DEMO

!   Using  Class  Loader    /  Dependency  InjecWon  /  Events  Manager  for  plugin-­‐able  applicaWons.  

!   Linux  Upstart  init  daemon  like  demo  hHp://upstart.ubuntu.com/    (replacement  for  the  venerable  System-­‐V  init)  

!   Shopping-­‐Cart  Plugin-­‐able  PromoWons  .  

Page 38: Phalcon   the fastest php framework 阿土伯

Micro  Framework

!   Simple  and  ResTul  

!   Frameworks  ! RubyàSinatra,  Cuba

! PythonàFlask,  Pyramid  !   PHP  àSilex,  Slim  !   Java  àNapalm  

Page 39: Phalcon   the fastest php framework 阿土伯

Micro  Example

Not  Found  Handler

Page 40: Phalcon   the fastest php framework 阿土伯

Full  Stack  Framework

!   MVC  Architecture  

!   Router  /  Dispatcher  

!   View  Helper  /  Template  Engine  

!   Filtering  Data  

!   Session    

!   Cache  

!   ACL  

!   Logging  

!   I18n/l10n  

Page 41: Phalcon   the fastest php framework 阿土伯

Models CreaWng  Models

By  default  model  “Robots”  will  refer  to  the  table  “robots”.

Page 42: Phalcon   the fastest php framework 阿土伯

Models Finding  Records

Page 43: Phalcon   the fastest php framework 阿土伯

Models Save  changes

Page 44: Phalcon   the fastest php framework 阿土伯

Models Finding  with  condiWons  ,  order  and  Limit

Page 45: Phalcon   the fastest php framework 阿土伯

Models

!   RelaWons  

Page 46: Phalcon   the fastest php framework 阿土伯

Models

!   RelaWons  

Page 47: Phalcon   the fastest php framework 阿土伯

Models

!   RelaWons  

Page 48: Phalcon   the fastest php framework 阿土伯

Models  -­‐  Criteria

!   Similar  To    !   Doctrine  QueryBuilder  ! Yii  Query  Object  

!   Provides  a  fluent  API.  

Page 49: Phalcon   the fastest php framework 阿土伯

PHQL

!   Similar  To    !   Doctrine  DQL  !   Hibernate  HQL  

! Phalcon  Query  Language  is  a  high  level,  object  oriented  SQL  dialect.  

!   High  performance,  Phalcon  provides  a  parser  that  uses  the  same  technology  as  SQLite.  (lemon  parser)  hHp://www.sqlite.org/src/doc/trunk/doc/lemon.html  

Page 50: Phalcon   the fastest php framework 阿土伯

PHQL

Select  Records

Select  Records  with  namespaces

Page 51: Phalcon   the fastest php framework 阿土伯

Controller

Page 52: Phalcon   the fastest php framework 阿土伯

View  Helpers

!   WriWng  and  maintaining  HTML  markup  can  quickly

Document  type

Produced

Page 53: Phalcon   the fastest php framework 阿土伯

Template  Engine  -­‐  Volt

!   Volt  is  an  ultra-­‐fast  and  designer  friendly  templaWng  language  wriHen  in  C  ,  inspired  on  Twig.

Page 54: Phalcon   the fastest php framework 阿土伯

Console  ApplicaWon

!   CLI  Components  

! cronjobs  

!   Daemon  ApplicaWon  !   Long-­‐life  applicaWon  ,  using  supervisor  and  kill  yourself.  

!   Job  Workers  !   Using  supervisor  and  kill  yourself.  

 

Page 55: Phalcon   the fastest php framework 阿土伯

CLI  Launcher

Page 56: Phalcon   the fastest php framework 阿土伯

CLI  MainTask

Php  console.php    //  MainTask::mainAcWon  

Php  console.php  main  test  //  MainTask::testAcWon  

Page 57: Phalcon   the fastest php framework 阿土伯

CLI  DEMO

Page 58: Phalcon   the fastest php framework 阿土伯

PhalconPHP  Kernel

! hHps://github.com/racklin/phalcon_kernel  

!   由 cphalcon  中抽離出來的專案。  

!   提供大量基於 Zend  API  的巨集/工具。  

!   簡化 php  extension  開發。  

Page 59: Phalcon   the fastest php framework 阿土伯

Why  Extension

!   Driver  /  Adapter  

!   Low-­‐level  NaWve  Code  Libraries  

!   Performance  

!   EncapsulaWon  /  InformaWon  Hidden  !   For  Developer  !   For  Customers    

Page 60: Phalcon   the fastest php framework 阿土伯

zval

Page 61: Phalcon   the fastest php framework 阿土伯

c:  zval  *a      相當於  php:  $a

Page 62: Phalcon   the fastest php framework 阿土伯

Data  Types

Long Boolean Double String  value

ZVAL_LONG() ZVAL_BOOL() ZVAL_DOUBLE() ZVAL_STRING()

NULL

ZVAL_NULL()

Create  macro

Page 63: Phalcon   the fastest php framework 阿土伯

Data  Types

Long Boolean Double String  value String  Length

Z_LVAL() Z_BVAL() Z_DVAL() Z_STRVAL() Z_STRLEN()

Z_LVAL_P() Z_BVAL_P() Z_DVAL_P() Z_STRVAL_P() Z_STRLEN_P()

HashTable Object

Z_ARRVAL() Z_OBJVAL()

Z_ARRVAL_P() Z_OBJVAL_P()

GeHer  and  IdenWfy  macro

Iden8fy   BOOL Long Double String NULL

Z_TYPE() IS_BOOL IS_LONG IS_DOUBLE IS_STRING IS_NULL

Z_TYPE_P()

Page 64: Phalcon   the fastest php framework 阿土伯

Return  Types

Macro Type

RETURN_FALSE  /  RETURN_TRUE Boolean

RETURN_NULL() NULL

RETURN_LONG() Long

RETURN_DOUBLE() Double

RETURN_STRING() String

RETURN_CTOR()  RETURN_CCTOR()  RETURN_NCTOR()

ZVAL  (Phalcon)

Page 65: Phalcon   the fastest php framework 阿土伯

Memory  Management

! Phalcon  Memory  Management  簡化了 Zend  Memory  Managemant.  

!   PHALCON_MM_GROW()  建立一個 phalcon  memory  entry  結構  

!   PHALCON_INIT_VAR  /  PHALCON_ALLOC_ZVAL_MM    建立並配置記憶體給 zval   並將其設為 null,  且將其指標的指標存至 phalcon  memory  entry.  

!   PHALCON_MM_RESTORE  釋放所有經由 PHALCON_INIT_VAR  配置且 refcount  =1  的記憶體

Page 66: Phalcon   the fastest php framework 阿土伯

Phalcon  MM  Bad  Parts

!   PHALCON_ALLOC_ZVAL_MM  or  PHALCON_INIT_VAR  !   如果你 trace  source  code  二者差別只在於一個是先將 zval  

pointer  pointer  放入 memory  entry  再設定初始值。一個是先設定初始值再將 zval  pointer  pointer  放入 memory  entry  ….    

!   PHALCON_INIT_VAR  3488  lines  !   PHALCON_ALLOC_ZVAL_MM      468  lines  

 

Page 67: Phalcon   the fastest php framework 阿土伯

MM  Sample

Page 68: Phalcon   the fastest php framework 阿土伯

String

!   ZVAL_STRING()  –  Zend  API  

 

   

! concat_funcWon  –  Zend  API  

! Concat  macro  

PHALCON_INIT_VAR(month);    ZVAL_STRING(month,  "2012",  1);  PHALCON_INIT_VAR(today_is);    ZVAL_STRING(today_is,  "Today  is",  1);

PHALCON_INIT_VAR(first_part);  concat_funcWon(first_part,  today_is,  month);

PHALCON_INIT_VAR(today);    PHALCON_CONCAT_SVSV(today,  "Today  is",  month,  "  ",  day);  

Page 69: Phalcon   the fastest php framework 阿土伯

Concat  Macros #define  PHALCON_CONCAT_SV(result,  op1,  op2)  \  #define  PHALCON_CONCAT_SVS(result,  op1,  op2,  op3)  \  #define  PHALCON_CONCAT_SVSV(result,  op1,  op2,  op3,  op4)  \  #define  PHALCON_CONCAT_SVSVS(result,  op1,  op2,  op3,  op4,  op5)  \  #define  PHALCON_CONCAT_SVSVSV(result,  op1,  op2,  op3,  op4,  op5,  op6)  \  #define  PHALCON_CONCAT_SVSVSVS(result,  op1,  op2,  op3,  op4,  op5,  op6,  op7)  \  #define  PHALCON_CONCAT_SVSVV(result,  op1,  op2,  op3,  op4,  op5)  \  #define  PHALCON_CONCAT_SVV(result,  op1,  op2,  op3)  \  #define  PHALCON_CONCAT_VS(result,  op1,  op2)  \  #define  PHALCON_CONCAT_VSV(result,  op1,  op2,  op3)  \  #define  PHALCON_CONCAT_VSVS(result,  op1,  op2,  op3,  op4)  \  #define  PHALCON_CONCAT_VSVSVS(result,  op1,  op2,  op3,  op4,  op5,  op6)  \  #define  PHALCON_CONCAT_VSVSVSV(result,  op1,  op2,  op3,  op4,  op5,  op6,  op7)  \  #define  PHALCON_CONCAT_VV(result,  op1,  op2)  \  #define  PHALCON_CONCAT_VVSV(result,  op1,  op2,  op3,  op4)  \  #define  PHALCON_CONCAT_VVV(result,  op1,  op2,  op3)  \  #define  PHALCON_CONCAT_VVVVV(result,  op1,  op2,  op3,  op4,  op5)  \

Page 70: Phalcon   the fastest php framework 阿土伯

Arrays

!    array_init(zval*)  –  iniWal  as  array  

!   Add  to  array  !    add_next_index_long    /  add_next_index_double  !    add_next_index_bool    /  add_next_index_stringl  

!   Get  (index  type)  (*string  length)  ! phalcon_array_fetch_long  /  phalcon_array_fetch_string  

!   Update  (index  type)  (*string  length)  ! phalcon_array_update_long  /  phalcon_array_update_string  

! Isset  /  unset    (*string  size)  !    phalcon_array_isset_long  /  phalcon_array_isset_string  !    phalcon_array_unset  /  phalcon_array_unset_string  

Page 71: Phalcon   the fastest php framework 阿土伯

Phalcon  Arrays  Bad  Parts

!   PHP  String  is  Binary-­‐Safe  !   String  Length  =  Actually  String  Length  !   String  Size  =  String  Length  +  1  bytes(\0)  

!   Arrays  fetch  and  update  using  String  Length  

!   Arrays  isset  and  unset  using  String  Size  

! Phalcon_array_unset  not  phalcon_array_unset_long

Page 72: Phalcon   the fastest php framework 阿土伯

Call  FuncWons

!   PHALCON_CALL_FUNC    呼叫 PHP  funcWon  ,有回傳值  

!   PHALCON_CALL_FUNC_NORETURN  呼叫 PHP  funcWon  ,沒有回傳值

!   PHALCON_CALL_FUNC_PARAMS_1    呼叫 PHP  funcWon 並傳一個參數,有回傳值  

!   PHALCON_CALL_FUNC_PARAMS_1_NORETURN  呼叫 PHP  funcWon 並傳一個參數,沒有回傳值  

!   目前巨集定義到傳 3 個參數  

Page 73: Phalcon   the fastest php framework 阿土伯

Call  FuncWons

PHP  Code

Page 74: Phalcon   the fastest php framework 阿土伯

Call  FuncWons

Page 75: Phalcon   the fastest php framework 阿土伯

Call  Methods

!   PHALCON_CALL_METHOD  呼叫 PHP  funcWon ,有回傳值  

!   PHALCON_CALL_METHOD_NORETURN  呼叫 PHP  funcWon ,沒有回傳值

!   PHALCON_CALL_METHOD_PARAMS_1    呼叫 PHP  funcWon 並傳一個參數,有回傳值  

!   PHALCON_CALL_METHOD_PARAMS_1_NORETURN  呼叫 PHP  funcWon 並傳一個參數,沒有回傳值  

!   目前巨集定義到傳 5  個參數  

Page 76: Phalcon   the fastest php framework 阿土伯

Call  Methods

Page 77: Phalcon   the fastest php framework 阿土伯

Object  ProperWes

!   Update  Property  !    phalcon_update_property_long  !    phalcon_update_property_bool  !    phalcon_update_property_null  !    phalcon_update_property_zval  /  

phalcon_update_property_zval_zval  

!   Read  Property  !    phalcon_read_property  /  phalcon_read_property_zval  

! Isset  Property  !    phalcon_isset_property  /  phalcon_isset_property_zval  

Page 78: Phalcon   the fastest php framework 阿土伯

PHP  Style WriWng  c  extension

Page 79: Phalcon   the fastest php framework 阿土伯

c9s  ValidaWonKit hHps://github.com/c9s/ValidaWonKit  

 c  extension  DEMO

Page 80: Phalcon   the fastest php framework 阿土伯

References

!   Slide:  hHps://speakerdeck.com/racklin/phalcon-­‐php-­‐framework-­‐and-­‐phalcon-­‐kernel-­‐introducWon-­‐phpcon�w2012  

!   Sample  code:  hHps://github.com/racklin/phpcon�w2012_phalcon  

! PhalconPHP  internal:    hHp://internals.phalconphp.com/en/latest/index.html  

Page 81: Phalcon   the fastest php framework 阿土伯

Thank  You