PHP.next:	
  Traits	
  

Horizontal	
  Reuse	
  of	
  Behavior	
  



                   Stefan	
  Marr	
  
            AFUP,	
  La	
  Can@ne,	
  Paris	
  
             15th	
  December	
  2010	
  
Ques@ons	
  at	
  any	
  @me	
  
Traits	
  for	
  PHP	
  Started	
  December	
  2007	
  




15/12/10	
                                         3	
  
Today:	
  Virtual	
  Machines	
  for	
  the	
  Manycore	
  Era	
  




                              hOp://soR.vub.ac.be/~smarr/research/	
  
                              hOp://github.com/smarr/RoarVM	
  




15/12/10	
                                                      4	
  
Who	
  are	
  you?	
  
•  PHP	
  Programmer:	
  Job	
  or	
  Hobby?	
  

•  C	
  Programmer?	
  (PHP	
  core/extensions?)	
  

•  Contribu@on	
  to	
  Open	
  Source	
  projects?	
  




15/12/10	
                                                5	
  
Off-­‐Topic…	
  
               Anyone	
  using:	
  
               •  COCOMO,	
  COCOMO	
  II	
  
               •  Func@on	
  points	
  
               •  User	
  Story	
  Points	
  




15/12/10	
                                      6	
  
Ques@ons	
  at	
  any	
  @me	
  
Agenda	
  
•    Why	
  Traits?	
  
•    The	
  Language	
  Design	
  
•    Traits	
  Applied	
  
•    Implementa@on	
  inside	
  the	
  Zend	
  Engine	
  




15/12/10	
                                                  8	
  
Reuse	
  in	
  Programming	
  Languages	
  

    WHY	
  TRAITS?	
  


15/12/10	
                         Stefan	
  Marr	
  –	
  PHP.next:	
  Traits	
     9	
  
Single	
  Inheritance	
  
                                                                             Vertebrates
•  Simple	
  but	
  not	
  	
  
   expressive	
  enough	
  

•  Leads	
  to	
                                           Dinosaurs                       Mammal

    •  Code	
  duplica@on	
  
    •  Inappropriate	
  	
  
       hierarchies	
                                     Archaeoptrix                          Bat


                                                        fly()                          fly()



15/12/10	
             Approaches	
  for	
  ComposiIon	
  and	
  their	
  Limits	
                   10	
  
Mul@ple	
  Inheritance	
  
                  Animal                                         •  Very	
  expressive	
  
                +move()

                                                                 •  But	
  
         Bird
                            Swimmer                                  •  Diamond	
  problem	
  
  +move()
                           +move()
                                                                     •  Fragile	
  hierarchies	
  
  +getLegs()



                  Penguin                                    class Penguin {
                                                               function move() {
                +move()                                          Swimmer::move();
                                                               }
                                                             }



15/12/10	
                    Approaches	
  for	
  ComposiIon	
  and	
  their	
  Limits	
     11	
  
Mixins	
  
                                                Animal




                                           WingAnimal
                                                                          <<mixin>> (1)   Bat
                  <<mixin>> (1)
                                       bones = light
                                       extremity = wings
        Penguin                        move()

                  <<mixin>> (2)            SwimAnimal
                                       extremity = fins
                                       move()




15/12/10	
              Approaches	
  for	
  ComposiIon	
  and	
  their	
  Limits	
             12	
  
Mixins	
  
                                                Animal




                                           WingAnimal
                                                                          <<mixin>> (1)    Bat
                  <<mixin>> (1)
                                       bones = light
                                       extremity = wings
        Penguin                        move()

                  <<mixin>> (2)            SwimAnimal
                                       extremity = fins                      •  Lack	
  of	
  composi@on	
  
                                       move()                                   control	
  
                                                                             •  Fragile	
  hierarchies	
  

15/12/10	
              Approaches	
  for	
  ComposiIon	
  and	
  their	
  Limits	
              13	
  
Limita@ons	
  
•    Code	
  duplica@on	
  
•    Inappropriate	
  hierarchies	
  
•    Diamond	
  problem	
  
•    Fragile	
  hierarchies	
  
•    Lack	
  of	
  composi@on	
  control	
  




15/12/10	
           Approaches	
  for	
  ComposiIon	
  and	
  their	
  Limits	
     14	
  
Traits	
  for	
  PHP	
  

    THE	
  LANGUAGE	
  DESIGN	
  


15/12/10	
                     Stefan	
  Marr	
  –	
  PHP.next:	
  Traits	
     15	
  
                                                      	
  
Traits	
  
•    Set	
  of	
  methods	
  
•    Possibly	
  composed	
  of	
  other	
  Traits	
  
•    Composi@on	
  is	
  unordered	
                                TFoo

•    Composi@on	
  opera@ons	
              TTrait
                                                               foo()

                                                                       TBar
       –  Sum	
                                  m1()
                                                 m2()          bar()
                          MyClass
       –  Exclusion	
                              TConflict
       –  Aliasing	
                             m2()

•  Can	
  be	
  flaOened	
  away	
  
15/12/10	
                          Traits	
                       16	
  
Traits	
  in	
  Ac@on	
  




15/12/10	
               Traits	
          17	
  
Method	
  in	
  Class	
  Overrides	
  Trait	
  
                              Base	
  
                         foo()	
  
                                                                   class	
  Base	
  {	
  
Code	
  




                                                                   	
  	
  function	
  foo()	
  {	
  echo	
  'B';}}	
  
                                 A	
                T	
            	
  
                         foo()	
            foo()	
                trait	
  T	
  {	
  
                                                                   	
  	
  function	
  foo()	
  {	
  echo	
  'T';}}	
  
                              Base	
                               	
  
Compiled	
  Result	
  




                         foo()	
                                   class	
  A	
  extends	
  Base	
  {	
  
                                                                   	
  	
  use	
  T;	
  
                                                                   	
  	
  function	
  foo()	
  {	
  echo	
  'A';}}	
  
                                 A	
  
                         foo()	
         echo	
  'A';	
  

                  15/12/10	
                                Traits	
                                    18	
  
Conflicts	
  are	
  Explicit	
  
                              Base	
  
                                                                                  class	
  Base	
  {	
  
                         foo()	
                                                  	
  	
  function	
  foo()	
  {	
  echo	
  'B';}}	
  
                                                         T1	
  
                                                                                  	
  
Code	
  




                                                  foo()	
                         trait	
  T1	
  {	
  
                                 A	
                                              	
  	
  function	
  foo()	
  {echo	
  'T1';}}	
  
                                                         T2	
                     	
  
                                                  foo()	
                         trait	
  T2	
  {	
  
                              Base	
                                              	
  	
  function	
  foo()	
  {echo	
  'T2';}}	
  
Compiled	
  Result	
  




                         foo()	
                                                  	
  
                                                                                  class	
  A	
  extends	
  Base	
  {	
  
                                                                                  	
  	
  use	
  T1,	
  T2;	
  
                                 A	
     >>	
  Fatal	
  error:	
  	
              }	
  
                                         	
  	
  	
  collision	
  on	
  foo	
  

                  15/12/10	
                                              Traits	
                                     19	
  
                                                                            	
  
Solving	
  Conflicts	
  
                              Base	
  
                                                                           class	
  Base	
  {	
  
                         foo()	
                                           	
  	
  function	
  foo()	
  {	
  echo	
  'B';}}	
  
                                                           T1	
  
                                                                           trait	
  T1	
  {	
  
Code	
  




                                                    foo()	
                	
  	
  function	
  foo()	
  {echo	
  'T1';}}	
  
                                 A	
                                       trait	
  T2	
  {	
  
                                                           T2	
            	
  	
  function	
  foo()	
  {echo	
  'T2';}}	
  
                                         !foo	
  
                                                    foo()	
                	
  
                              Base	
                                       class	
  A	
  extends	
  Base	
  {	
  
Compiled	
  Result	
  




                         foo()	
                                           	
  	
  use	
  T1,	
  T2	
  {	
  
                                                                           	
  	
  	
  	
  T1::foo	
  insteadof	
  T2;	
  
                                                                           	
  	
  }	
  
                                 A	
                                       }	
  
                         foo()	
         >>	
  echo	
  'T1';	
  


                  15/12/10	
                                        Traits	
                                    20	
  
                                                                      	
  
Aliasing	
  
                              Base	
  
                                                                               class	
  Base	
  {	
  
                         foo()	
                                               	
  	
  function	
  foo()	
  {	
  echo	
  'B';}}	
  
                                                               T1	
  
                                                                               trait	
  T1	
  {	
  
Code	
  




                                                        foo()	
                	
  	
  function	
  foo()	
  {echo	
  'T1';}}	
  
                                 A	
                                           trait	
  T2	
  {	
  
                                         !foo	
                T2	
            	
  	
  function	
  foo()	
  {echo	
  'T2';}}	
  
                                         +foo2	
  
                                                        foo()	
                	
  
                              Base	
                                           class	
  A	
  extends	
  Base	
  {	
  
Compiled	
  Result	
  




                         foo()	
                                               	
  	
  use	
  T1,	
  T2	
  {	
  
                                                                               	
  	
  	
  	
  T1::foo	
  insteadof	
  T2;	
  
                                                                               	
  	
  	
  	
  T2::foo	
  as	
  fooT2;	
  
                              A	
  
                         foo()	
         >>	
  echo	
  'T1';	
  
                                                                               	
  	
  }	
  
                         foo2()	
        >>	
  echo	
  'T2';	
                 }	
  

                  15/12/10	
                                            Traits	
                                    21	
  
                                                                          	
  
Aliasing	
  IS	
  NOT	
  Renaming	
  
trait	
  T1	
  {	
                            class	
  A	
  {	
  
	
  	
  function	
  bar()	
  {	
  
	
  	
  	
  	
  echo	
  'T1::bar';	
  
                                              	
  	
  use	
  T1,	
  T2	
  {	
  
	
  	
  }	
                                   	
  	
  	
  	
  T2::bar	
  insteadof	
  T1;	
  
	
  	
  function	
  foo()	
  {	
              	
  	
  	
  	
  T1::bar	
  as	
  bar2;	
  
	
  	
  	
  	
  $this-­‐>bar();	
  
                                              	
  	
  }	
  
	
  	
  }	
  
}	
                                           }	
  
trait	
  T2	
  {	
                            	
  
	
  	
  function	
  bar()	
  {	
  
                                              $a	
  =	
  new	
  A;	
  
	
  	
  	
  	
  echo	
  'T2::bar’;	
  
	
  	
  }	
                                   $a-­‐>foo();	
  //	
  echo	
  ??	
  
}	
  

15/12/10	
                               Traits	
                               22	
  
                                           	
  
Late	
  Binding	
  for	
  Composability	
  
trait	
  MovingAnimal	
  {	
                            class	
  Penguin	
  {	
  
	
  	
  function	
  move()	
  {	
                       	
  	
  use	
  WingAnimal,	
  SwimAnimal	
  {	
  
	
  	
  	
  	
  $this-­‐>extremities()-­‐>use();	
      	
  	
  	
  	
  WingAnimal::extremities	
  
	
  	
  }}	
                                            	
  	
  	
  	
  	
  	
  insteadof	
  SwimAnimal;	
  
trait	
  WingAnimal	
  {	
                              	
  	
  }	
  
	
  	
  use	
  MovingAnimal;	
                          }	
  
	
  	
  function	
  extremities()	
  {	
                	
  
	
  	
  	
  	
  return	
  $this-­‐>wings;	
             $skipper	
  =	
  new	
  Penguin;	
  
	
  	
  }}	
                                            $skipper-­‐>move();	
  //	
  will	
  use	
  
trait	
  SwimAnimal	
  {	
                              	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  his	
  wings	
  
	
  	
  use	
  MovingAnimal;	
  
	
  	
  function	
  extremities()	
  {	
  
	
  	
  	
  	
  return	
  $this-­‐>fins;	
  
	
  	
  }}	
  

 15/12/10	
                                      Traits	
                                                                               23	
  
                                                   	
  
Traits	
  and	
  State	
  
trait	
  Counter	
  {	
  
	
  	
  abstract	
  function	
  &getValueVar();	
  
	
  	
  function	
  inc($inc	
  =	
  1)	
  {	
  
	
  	
  	
  	
  $var	
  =	
  &$this-­‐>getValueVar();	
  
	
  	
  	
  	
  $var	
  =	
  $var	
  +	
  $inc;	
  
	
  	
  }}	
  
class	
  PageWithCounter	
  {	
  
	
  	
  use	
  Counter;	
  
	
  	
  public	
  $counter	
  =	
  1;	
  
	
  	
  function	
  &getValueVar()	
  {	
  return	
  $this-­‐>counter;	
  }	
  
}	
  
$page	
  =	
  new	
  PageWithCounter;	
  $page-­‐>inc();	
  
var_dump($page-­‐>counter);	
  


 15/12/10	
                                   Traits	
                            24	
  
                                                	
  
Case	
  Studies	
  Illustra@ng	
  Benefits	
  of	
  Traits	
  

    TRAITS	
  APPLIED	
  


15/12/10	
                              Stefan	
  Marr	
  –	
  PHP.next:	
  Traits	
     25	
  
                                                               	
  
Use	
  Case	
  for	
  Traits	
  
•  ezcReflec@on	
  enhances	
  PHP	
  Reflec@on	
  API	
  
       –  Adds	
  XDoclet	
  like	
  annota@ons	
  for	
  PHP	
  
       –  Enhances	
  type	
  hin@ng	
  using	
  PHPDoc	
  type	
  
          annota@ons	
  
•  Illustrates	
  solu@on	
  for	
  code	
  duplica@on	
  
   problems	
                                  «interface»
                                                Reflector




           ReflectionProperty   ReflectionClass             ReflectionFunction   ReflectionParameter


                                ReflectionObject             ReflectionMethod



15/12/10	
                                   Traits	
  Applied	
                                26	
  
Refactoring	
  ezcReflec@on	
  with	
  Traits	
  




15/12/10	
              Traits	
  Applied	
     27	
  
Refactoring	
  ezcReflec@on	
  with	
  Traits	
  




15/12/10	
              Traits	
  Applied	
     28	
  
Implementa@on	
  details:	
  
           Not	
  to	
  be	
  confused	
  with	
  language	
  seman@cs!	
  




    The	
  Traits	
  Implementa@on	
  

    INSIDE	
  THE	
  ZEND	
  ENGINE	
  


15/12/10	
                         Stefan	
  Marr	
  –	
  PHP.next:	
  Traits	
     29	
  
                                                          	
  
Zend	
  Engine	
  Overview	
  
                                    PHP	
  +	
  Zend	
  Engine	
  

                 YACC	
                    Opcodes	
  
 Script	
        Parser	
                 Structures	
  

                                                                             Opcode	
  
                                                                           Interpreter	
         Result	
  

 Input	
                      Input	
  




15/12/10	
                       Traits	
  inside	
  the	
  Zend	
  Engine	
                 30	
  
YACC	
  
                                  Parser	
                                          Parser	
  



•  Added	
  keywords	
  
       –  trait	
  
       –  insteadof	
  


•  Grammar	
  rules	
  based	
  on	
  class	
  	
  




15/12/10	
                 Traits	
  inside	
  the	
  Zend	
  Engine	
     31	
  
                                                	
  
PHP	
  +	
  Zend	
  
                                                                                   Engine	
  
                               Zend	
  Engine	
  
•  Trait	
  internally	
  a	
  class	
  with	
  ZEND_ACC_TRAIT	
  
       –  New	
  data	
  structures	
  
               •  zend_trait_method_reference	
  
               •  zend_trait_precedence	
  
               •  zend_trait_alias	
  
       –  Class	
  members	
  added	
  to	
  zend_class_entry	
  
               •    zend_class_entry	
  **traits;	
  
               •    zend_uint	
  num_traits;	
  
               •    zend_trait_alias	
  **trait_aliases;	
  
               •    zend_trait_precedence	
  **trait_precedences;	
  
•  Support	
  in	
  Reflec@on	
  API	
  
15/12/10	
                       Traits	
  inside	
  the	
  Zend	
  Engine	
       32	
  
                                                      	
  
Opcode	
  
                                         Interpreter	
                                       Interpreter	
  



•  Added	
  Opcodes	
  
       –  ZEND_ADD_TRAIT	
  
               •  similar	
  to	
  ZEND_ADD_INTERFACE	
  
               •  Only	
  adds	
  info,	
  no	
  composi@on	
  done	
  
       –  ZEND_BIND_TRAITS	
  
               •  emiOed	
  in	
  zend_do_end_class_declara@on	
  
               •  will	
  ini@ate	
  trait	
  composi@on	
  at	
  run@me	
  
                    –  Only	
  once,	
  part	
  of	
  class	
  defini@on	
  process	
  	
  
                    –  (no	
  run@me	
  overhead)	
  


15/12/10	
                                 Traits	
  inside	
  the	
  Zend	
  Engine	
        33	
  
                                                                	
  
PHP	
  +	
  Zend	
  
                                                                                  Engine	
  
                   Composi@on	
  Process	
  
•  Implemented	
  in	
  zend_do_bind_traits	
  
1.  Init	
  traits	
  data	
  structures	
  (resolve	
  classes)	
  
2.  For	
  all	
  used	
  traits	
  
      –  Compile	
  method	
  table	
  of	
  all	
  used	
  methods	
  
      –  Respect	
  exclusion	
  and	
  aliases	
  
3.  Merge	
  resul@ng	
  tables	
  
4.  Add	
  methods	
  to	
  class	
  
      –  Handle	
  abstract	
  methods,	
  inheritance	
  
      –  duplicate	
  func@on	
  (like	
  copy/paste,	
  enables	
  static)	
  

15/12/10	
                      Traits	
  inside	
  the	
  Zend	
  Engine	
       34	
  
                                                     	
  
ROUNDUP	
  AND	
  CONCLUSION	
  


15/12/10	
       Stefan	
  Marr	
  –	
  PHP.next:	
  Traits	
     35	
  
                                        	
  
References	
  for	
  Traits	
  in	
  PHP	
  
RFC:	
  Horizontal	
  Reuse	
  in	
  PHP	
  
       Version	
  2.0.1,	
  last	
  update	
  2010-­‐11-­‐18	
  
       hOp://wiki.php.net/rfc/horizontalreuse	
  
	
  
Interes@ng	
  Blog	
  Posts	
  
       –  Simas	
  Toleikis:	
  Overview,	
  similar	
  to	
  RFC	
  
               •  hOp://simas.posterous.com/new-­‐to-­‐php-­‐54-­‐traits	
  
       –  Sebas@an	
  Deutsch:	
  Ac@veRecord	
  with	
  Traits	
  
               •  hOp://9elements.com/io/?p=28	
  

15/12/10	
                         Roundup	
  and	
  Conclusion	
         36	
  
                                                	
  
Conclusion	
  
•  Traits	
  are	
  mechanism	
  for	
  reuse	
  of	
  behavior	
  
•  Avoid	
  limita@ons	
  of	
  other	
  approaches	
  
       –  Avoid	
  code	
  duplica@on	
  
       –  Enhance	
  class	
  composi@on	
  capabili@es	
  
•  “Compiler	
  assisted	
  copy’n’paste”	
  
       –  FlaOened	
  into	
  class	
  
       –  No	
  run@me	
  overhead	
  


15/12/10	
                     Roundup	
  and	
  Conclusion	
     37	
  
                                            	
  
Basic	
  Literature	
  on	
  Traits	
  
Main	
  ArIcle	
  about	
  Traits	
  	
  
[3] 	
  S.	
  DUCASSE,	
  O.	
  NIERSTRASZ,	
  N.	
  SCHÄRLI,	
  R.	
  WUYTS,	
  AND	
  A.	
  P.	
  
        BLACK,	
  Traits:	
  A	
  Mechanism	
  for	
  Fine-­‐Grained	
  Reuse,	
  ACM	
  
        Trans.	
  Program.	
  Lang.	
  Syst.,	
  28	
  (2006),	
  pp.	
  331–388.	
  
	
  
Traits	
  Extensions	
  
[1]       	
  A.	
  BERGEL,	
  S.	
  DUCASSE,	
  O.	
  NIERSTRASZ,	
  AND	
  R.	
  WUYTS,	
  Stateful	
  Traits	
  and	
  their	
  
              Formaliza;on,	
  Journal	
  of	
  Computer	
  Languages,	
  Systems	
  and	
  Structures,	
  34	
  
              (2007),	
  pp.	
  83–108.	
  
[4]       	
  S.	
  DUCASSE,	
  R.	
  WUYTS,	
  A.	
  BERGEL,	
  AND	
  O.	
  NIERSTRASZ,	
  User-­‐Changeable	
  Visibility:	
  
              Resolving	
  Unan;cipated	
  Name	
  Clashes	
  in	
  Traits,	
  SIGPLAN	
  Not.,	
  42	
  (2007),	
  
              pp.	
  171–190.	
  

	
  
	
  
15/12/10	
                                        Roundup	
  and	
  Conclusion	
  
                                                               	
  
                                                                                                                         38	
  
Traits	
  Literature	
  
[1]A.	
  BERGEL,	
  S.	
  DUCASSE,	
  O.	
  NIERSTRASZ,	
  AND	
  R.	
  WUYTS,	
  Stateful	
  Traits	
  and	
  their	
  
  	
  
     Formaliza;on,	
  Journal	
  of	
  Computer	
  Languages,	
  Systems	
  and	
  Structures,	
  34	
  (2007),	
  
     pp.	
  83–108.	
  
[2]S.	
  DENIER,	
  Traits	
  Programming	
  with	
  AspectJ,	
  RSTI	
  -­‐	
  L'objet,	
  11	
  (2005),	
  pp.	
  69–86.	
  
  	
  
[3]S.	
  DUCASSE,	
  O.	
  NIERSTRASZ,	
  N.	
  SCHÄRLI,	
  R.	
  WUYTS,	
  AND	
  A.	
  P.	
  BLACK,	
  Traits:	
  A	
  Mechanism	
  
  	
  
     for	
  Fine-­‐Grained	
  Reuse,	
  ACM	
  Trans.	
  Program.	
  Lang.	
  Syst.,	
  28	
  (2006),	
  pp.	
  331–388.	
  
[4]S.	
  DUCASSE,	
  R.	
  WUYTS,	
  A.	
  BERGEL,	
  AND	
  O.	
  NIERSTRASZ,	
  User-­‐Changeable	
  Visibility:	
  
  	
  
     Resolving	
  Unan;cipated	
  Name	
  Clashes	
  in	
  Traits,	
  SIGPLAN	
  Not.,	
  42	
  (2007),	
  pp.	
  171–
     190.	
  
[5]S.	
  MARR	
  AND	
  F.	
  MENGE,	
  ezcReflec;on,	
  SVN	
  Repository,	
  eZ	
  Components,	
  January	
  
  	
  
     2008.	
  hOp://svn.ez.no/svn/ezcomponents/experimental/Reflec@on.	
  
[6]E.	
  R.	
  MURPHY-­‐HILL,	
  P.	
  J.	
  QUITSLUND,	
  AND	
  A.	
  P.	
  BLACK,	
  Removing	
  Duplica;on	
  from	
  
  	
  
     java.io:	
  a	
  Case	
  Study	
  using	
  Traits,	
  in	
  OOPSLA	
  '05:	
  Companion	
  to	
  the	
  20th	
  annual	
  
     ACM	
  SIGPLAN	
  conference	
  on	
  Object-­‐oriented	
  programming,	
  systems,	
  languages,	
  
     and	
  applica@ons,	
  New	
  York,	
  NY,	
  USA,	
  2005,	
  ACM,	
  pp.	
  282–291.	
  




15/12/10	
                                         Roundup	
  and	
  Conclusion	
                                         39	
  
Traits	
  Literature	
  
[7] 	
  PROGRAMMING	
  METHODS	
  LABORATORY,	
  Traits	
  for	
  Scala,	
  Programming	
  
        Language,	
  Ecole	
  Polytechnique	
  Fédérale	
  de	
  Lausanne,	
  2006.	
  hOp://
        www.scala-­‐lang.org/intro/traits.html.	
  
[8] 	
  P.	
  J.	
  QUITSLUND,	
  E.	
  R.	
  MURPHY-­‐HILL,	
  AND	
  A.	
  P.	
  BLACK,	
  Suppor;ng	
  Java	
  traits	
  in	
  
        Eclipse,	
  in	
  eclipse	
  '04:	
  Proceedings	
  of	
  the	
  2004	
  OOPSLA	
  workshop	
  on	
  
        eclipse	
  technology	
  eXchange,	
  New	
  York,	
  NY,	
  USA,	
  2004,	
  ACM,	
  pp.	
  37–41.	
  
[9] 	
  S.	
  REICHHART,	
  Traits	
  in	
  C#.	
  Video	
  of	
  Talk	
  at	
  MicrosoR	
  Research,	
  September	
  
        2005.	
  
[10]	
  N.	
  SCHÄRLI,	
  Traits	
  —	
  Composing	
  Classes	
  from	
  Behavioral	
  Building	
  Blocks,	
  
        PhD	
  thesis,	
  University	
  of	
  Berne,	
  Feb.	
  2005.	
  
[11]	
  SOFTWARE	
  COMPOSITION	
  GROUP,	
  Traits	
  for	
  Squeak,	
  Smalltalk	
  VM,	
  University	
  
        of	
  Berne,	
  December	
  2006.	
  hOp://www.iam.unibe.ch/	
  scg/Research/
        Traits/index.html.	
  
[12]	
  L.	
  WALL,	
  Apocalypse	
  12:	
  Class	
  Composi;on	
  with	
  Roles,	
  tech.	
  report,	
  The	
  
        Perl	
  Founda@on,	
  2004.	
  hOp://www.perl.com/pub/a/2004/04/16/
        a12.html?page=11.	
  


15/12/10	
                                       Roundup	
  and	
  Conclusion	
                                     40	
  

PHP.next: Traits

  • 1.
    PHP.next:  Traits   Horizontal  Reuse  of  Behavior   Stefan  Marr   AFUP,  La  Can@ne,  Paris   15th  December  2010  
  • 2.
  • 3.
    Traits  for  PHP  Started  December  2007   15/12/10   3  
  • 4.
    Today:  Virtual  Machines  for  the  Manycore  Era   hOp://soR.vub.ac.be/~smarr/research/   hOp://github.com/smarr/RoarVM   15/12/10   4  
  • 5.
    Who  are  you?   •  PHP  Programmer:  Job  or  Hobby?   •  C  Programmer?  (PHP  core/extensions?)   •  Contribu@on  to  Open  Source  projects?   15/12/10   5  
  • 6.
    Off-­‐Topic…   Anyone  using:   •  COCOMO,  COCOMO  II   •  Func@on  points   •  User  Story  Points   15/12/10   6  
  • 7.
  • 8.
    Agenda   •  Why  Traits?   •  The  Language  Design   •  Traits  Applied   •  Implementa@on  inside  the  Zend  Engine   15/12/10   8  
  • 9.
    Reuse  in  Programming  Languages   WHY  TRAITS?   15/12/10   Stefan  Marr  –  PHP.next:  Traits   9  
  • 10.
    Single  Inheritance   Vertebrates •  Simple  but  not     expressive  enough   •  Leads  to   Dinosaurs Mammal •  Code  duplica@on   •  Inappropriate     hierarchies   Archaeoptrix Bat fly() fly() 15/12/10   Approaches  for  ComposiIon  and  their  Limits   10  
  • 11.
    Mul@ple  Inheritance   Animal •  Very  expressive   +move() •  But   Bird Swimmer •  Diamond  problem   +move() +move() •  Fragile  hierarchies   +getLegs() Penguin class Penguin { function move() { +move() Swimmer::move(); } } 15/12/10   Approaches  for  ComposiIon  and  their  Limits   11  
  • 12.
    Mixins   Animal WingAnimal <<mixin>> (1) Bat <<mixin>> (1) bones = light extremity = wings Penguin move() <<mixin>> (2) SwimAnimal extremity = fins move() 15/12/10   Approaches  for  ComposiIon  and  their  Limits   12  
  • 13.
    Mixins   Animal WingAnimal <<mixin>> (1) Bat <<mixin>> (1) bones = light extremity = wings Penguin move() <<mixin>> (2) SwimAnimal extremity = fins •  Lack  of  composi@on   move() control   •  Fragile  hierarchies   15/12/10   Approaches  for  ComposiIon  and  their  Limits   13  
  • 14.
    Limita@ons   •  Code  duplica@on   •  Inappropriate  hierarchies   •  Diamond  problem   •  Fragile  hierarchies   •  Lack  of  composi@on  control   15/12/10   Approaches  for  ComposiIon  and  their  Limits   14  
  • 15.
    Traits  for  PHP   THE  LANGUAGE  DESIGN   15/12/10   Stefan  Marr  –  PHP.next:  Traits   15    
  • 16.
    Traits   •  Set  of  methods   •  Possibly  composed  of  other  Traits   •  Composi@on  is  unordered   TFoo •  Composi@on  opera@ons   TTrait foo() TBar –  Sum   m1() m2() bar() MyClass –  Exclusion   TConflict –  Aliasing   m2() •  Can  be  flaOened  away   15/12/10   Traits   16  
  • 17.
    Traits  in  Ac@on   15/12/10   Traits   17  
  • 18.
    Method  in  Class  Overrides  Trait   Base   foo()   class  Base  {   Code      function  foo()  {  echo  'B';}}   A   T     foo()   foo()   trait  T  {      function  foo()  {  echo  'T';}}   Base     Compiled  Result   foo()   class  A  extends  Base  {      use  T;      function  foo()  {  echo  'A';}}   A   foo()   echo  'A';   15/12/10   Traits   18  
  • 19.
    Conflicts  are  Explicit   Base   class  Base  {   foo()      function  foo()  {  echo  'B';}}   T1     Code   foo()   trait  T1  {   A      function  foo()  {echo  'T1';}}   T2     foo()   trait  T2  {   Base      function  foo()  {echo  'T2';}}   Compiled  Result   foo()     class  A  extends  Base  {      use  T1,  T2;   A   >>  Fatal  error:     }        collision  on  foo   15/12/10   Traits   19    
  • 20.
    Solving  Conflicts   Base   class  Base  {   foo()      function  foo()  {  echo  'B';}}   T1   trait  T1  {   Code   foo()      function  foo()  {echo  'T1';}}   A   trait  T2  {   T2      function  foo()  {echo  'T2';}}   !foo   foo()     Base   class  A  extends  Base  {   Compiled  Result   foo()      use  T1,  T2  {          T1::foo  insteadof  T2;      }   A   }   foo()   >>  echo  'T1';   15/12/10   Traits   20    
  • 21.
    Aliasing   Base   class  Base  {   foo()      function  foo()  {  echo  'B';}}   T1   trait  T1  {   Code   foo()      function  foo()  {echo  'T1';}}   A   trait  T2  {   !foo   T2      function  foo()  {echo  'T2';}}   +foo2   foo()     Base   class  A  extends  Base  {   Compiled  Result   foo()      use  T1,  T2  {          T1::foo  insteadof  T2;          T2::foo  as  fooT2;   A   foo()   >>  echo  'T1';      }   foo2()   >>  echo  'T2';   }   15/12/10   Traits   21    
  • 22.
    Aliasing  IS  NOT  Renaming   trait  T1  {   class  A  {      function  bar()  {          echo  'T1::bar';      use  T1,  T2  {      }          T2::bar  insteadof  T1;      function  foo()  {          T1::bar  as  bar2;          $this-­‐>bar();      }      }   }   }   trait  T2  {        function  bar()  {   $a  =  new  A;          echo  'T2::bar’;      }   $a-­‐>foo();  //  echo  ??   }   15/12/10   Traits   22    
  • 23.
    Late  Binding  for  Composability   trait  MovingAnimal  {   class  Penguin  {      function  move()  {      use  WingAnimal,  SwimAnimal  {          $this-­‐>extremities()-­‐>use();          WingAnimal::extremities      }}              insteadof  SwimAnimal;   trait  WingAnimal  {      }      use  MovingAnimal;   }      function  extremities()  {            return  $this-­‐>wings;   $skipper  =  new  Penguin;      }}   $skipper-­‐>move();  //  will  use   trait  SwimAnimal  {                                            his  wings      use  MovingAnimal;      function  extremities()  {          return  $this-­‐>fins;      }}   15/12/10   Traits   23    
  • 24.
    Traits  and  State   trait  Counter  {      abstract  function  &getValueVar();      function  inc($inc  =  1)  {          $var  =  &$this-­‐>getValueVar();          $var  =  $var  +  $inc;      }}   class  PageWithCounter  {      use  Counter;      public  $counter  =  1;      function  &getValueVar()  {  return  $this-­‐>counter;  }   }   $page  =  new  PageWithCounter;  $page-­‐>inc();   var_dump($page-­‐>counter);   15/12/10   Traits   24    
  • 25.
    Case  Studies  Illustra@ng  Benefits  of  Traits   TRAITS  APPLIED   15/12/10   Stefan  Marr  –  PHP.next:  Traits   25    
  • 26.
    Use  Case  for  Traits   •  ezcReflec@on  enhances  PHP  Reflec@on  API   –  Adds  XDoclet  like  annota@ons  for  PHP   –  Enhances  type  hin@ng  using  PHPDoc  type   annota@ons   •  Illustrates  solu@on  for  code  duplica@on   problems   «interface» Reflector ReflectionProperty ReflectionClass ReflectionFunction ReflectionParameter ReflectionObject ReflectionMethod 15/12/10   Traits  Applied   26  
  • 27.
    Refactoring  ezcReflec@on  with  Traits   15/12/10   Traits  Applied   27  
  • 28.
    Refactoring  ezcReflec@on  with  Traits   15/12/10   Traits  Applied   28  
  • 29.
    Implementa@on  details:   Not  to  be  confused  with  language  seman@cs!   The  Traits  Implementa@on   INSIDE  THE  ZEND  ENGINE   15/12/10   Stefan  Marr  –  PHP.next:  Traits   29    
  • 30.
    Zend  Engine  Overview   PHP  +  Zend  Engine   YACC   Opcodes   Script   Parser   Structures   Opcode   Interpreter   Result   Input   Input   15/12/10   Traits  inside  the  Zend  Engine   30  
  • 31.
    YACC   Parser   Parser   •  Added  keywords   –  trait   –  insteadof   •  Grammar  rules  based  on  class     15/12/10   Traits  inside  the  Zend  Engine   31    
  • 32.
    PHP  +  Zend   Engine   Zend  Engine   •  Trait  internally  a  class  with  ZEND_ACC_TRAIT   –  New  data  structures   •  zend_trait_method_reference   •  zend_trait_precedence   •  zend_trait_alias   –  Class  members  added  to  zend_class_entry   •  zend_class_entry  **traits;   •  zend_uint  num_traits;   •  zend_trait_alias  **trait_aliases;   •  zend_trait_precedence  **trait_precedences;   •  Support  in  Reflec@on  API   15/12/10   Traits  inside  the  Zend  Engine   32    
  • 33.
    Opcode   Interpreter   Interpreter   •  Added  Opcodes   –  ZEND_ADD_TRAIT   •  similar  to  ZEND_ADD_INTERFACE   •  Only  adds  info,  no  composi@on  done   –  ZEND_BIND_TRAITS   •  emiOed  in  zend_do_end_class_declara@on   •  will  ini@ate  trait  composi@on  at  run@me   –  Only  once,  part  of  class  defini@on  process     –  (no  run@me  overhead)   15/12/10   Traits  inside  the  Zend  Engine   33    
  • 34.
    PHP  +  Zend   Engine   Composi@on  Process   •  Implemented  in  zend_do_bind_traits   1.  Init  traits  data  structures  (resolve  classes)   2.  For  all  used  traits   –  Compile  method  table  of  all  used  methods   –  Respect  exclusion  and  aliases   3.  Merge  resul@ng  tables   4.  Add  methods  to  class   –  Handle  abstract  methods,  inheritance   –  duplicate  func@on  (like  copy/paste,  enables  static)   15/12/10   Traits  inside  the  Zend  Engine   34    
  • 35.
    ROUNDUP  AND  CONCLUSION   15/12/10   Stefan  Marr  –  PHP.next:  Traits   35    
  • 36.
    References  for  Traits  in  PHP   RFC:  Horizontal  Reuse  in  PHP   Version  2.0.1,  last  update  2010-­‐11-­‐18   hOp://wiki.php.net/rfc/horizontalreuse     Interes@ng  Blog  Posts   –  Simas  Toleikis:  Overview,  similar  to  RFC   •  hOp://simas.posterous.com/new-­‐to-­‐php-­‐54-­‐traits   –  Sebas@an  Deutsch:  Ac@veRecord  with  Traits   •  hOp://9elements.com/io/?p=28   15/12/10   Roundup  and  Conclusion   36    
  • 37.
    Conclusion   •  Traits  are  mechanism  for  reuse  of  behavior   •  Avoid  limita@ons  of  other  approaches   –  Avoid  code  duplica@on   –  Enhance  class  composi@on  capabili@es   •  “Compiler  assisted  copy’n’paste”   –  FlaOened  into  class   –  No  run@me  overhead   15/12/10   Roundup  and  Conclusion   37    
  • 38.
    Basic  Literature  on  Traits   Main  ArIcle  about  Traits     [3]  S.  DUCASSE,  O.  NIERSTRASZ,  N.  SCHÄRLI,  R.  WUYTS,  AND  A.  P.   BLACK,  Traits:  A  Mechanism  for  Fine-­‐Grained  Reuse,  ACM   Trans.  Program.  Lang.  Syst.,  28  (2006),  pp.  331–388.     Traits  Extensions   [1]  A.  BERGEL,  S.  DUCASSE,  O.  NIERSTRASZ,  AND  R.  WUYTS,  Stateful  Traits  and  their   Formaliza;on,  Journal  of  Computer  Languages,  Systems  and  Structures,  34   (2007),  pp.  83–108.   [4]  S.  DUCASSE,  R.  WUYTS,  A.  BERGEL,  AND  O.  NIERSTRASZ,  User-­‐Changeable  Visibility:   Resolving  Unan;cipated  Name  Clashes  in  Traits,  SIGPLAN  Not.,  42  (2007),   pp.  171–190.       15/12/10   Roundup  and  Conclusion     38  
  • 39.
    Traits  Literature   [1]A.  BERGEL,  S.  DUCASSE,  O.  NIERSTRASZ,  AND  R.  WUYTS,  Stateful  Traits  and  their     Formaliza;on,  Journal  of  Computer  Languages,  Systems  and  Structures,  34  (2007),   pp.  83–108.   [2]S.  DENIER,  Traits  Programming  with  AspectJ,  RSTI  -­‐  L'objet,  11  (2005),  pp.  69–86.     [3]S.  DUCASSE,  O.  NIERSTRASZ,  N.  SCHÄRLI,  R.  WUYTS,  AND  A.  P.  BLACK,  Traits:  A  Mechanism     for  Fine-­‐Grained  Reuse,  ACM  Trans.  Program.  Lang.  Syst.,  28  (2006),  pp.  331–388.   [4]S.  DUCASSE,  R.  WUYTS,  A.  BERGEL,  AND  O.  NIERSTRASZ,  User-­‐Changeable  Visibility:     Resolving  Unan;cipated  Name  Clashes  in  Traits,  SIGPLAN  Not.,  42  (2007),  pp.  171– 190.   [5]S.  MARR  AND  F.  MENGE,  ezcReflec;on,  SVN  Repository,  eZ  Components,  January     2008.  hOp://svn.ez.no/svn/ezcomponents/experimental/Reflec@on.   [6]E.  R.  MURPHY-­‐HILL,  P.  J.  QUITSLUND,  AND  A.  P.  BLACK,  Removing  Duplica;on  from     java.io:  a  Case  Study  using  Traits,  in  OOPSLA  '05:  Companion  to  the  20th  annual   ACM  SIGPLAN  conference  on  Object-­‐oriented  programming,  systems,  languages,   and  applica@ons,  New  York,  NY,  USA,  2005,  ACM,  pp.  282–291.   15/12/10   Roundup  and  Conclusion   39  
  • 40.
    Traits  Literature   [7]  PROGRAMMING  METHODS  LABORATORY,  Traits  for  Scala,  Programming   Language,  Ecole  Polytechnique  Fédérale  de  Lausanne,  2006.  hOp:// www.scala-­‐lang.org/intro/traits.html.   [8]  P.  J.  QUITSLUND,  E.  R.  MURPHY-­‐HILL,  AND  A.  P.  BLACK,  Suppor;ng  Java  traits  in   Eclipse,  in  eclipse  '04:  Proceedings  of  the  2004  OOPSLA  workshop  on   eclipse  technology  eXchange,  New  York,  NY,  USA,  2004,  ACM,  pp.  37–41.   [9]  S.  REICHHART,  Traits  in  C#.  Video  of  Talk  at  MicrosoR  Research,  September   2005.   [10]  N.  SCHÄRLI,  Traits  —  Composing  Classes  from  Behavioral  Building  Blocks,   PhD  thesis,  University  of  Berne,  Feb.  2005.   [11]  SOFTWARE  COMPOSITION  GROUP,  Traits  for  Squeak,  Smalltalk  VM,  University   of  Berne,  December  2006.  hOp://www.iam.unibe.ch/  scg/Research/ Traits/index.html.   [12]  L.  WALL,  Apocalypse  12:  Class  Composi;on  with  Roles,  tech.  report,  The   Perl  Founda@on,  2004.  hOp://www.perl.com/pub/a/2004/04/16/ a12.html?page=11.   15/12/10   Roundup  and  Conclusion   40