traits: a new language feature for php?
DESCRIPTION
TRANSCRIPT
![Page 1: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/1.jpg)
Please,feel free to interrupt me at any time.
![Page 2: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/2.jpg)
Do you use:• Multiple Inheritance?• Mixins?
![Page 3: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/3.jpg)
Traits
A Language Feature for PHP?
Stefan MarrPHP Unconference Hamburg
26-27 April 2008
![Page 4: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/4.jpg)
Agenda
1. Introduction to Traits
2. Traits Applied
3. Traits for PHP: Trait versus Graft
4. Roundup and Conclusion
![Page 5: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/5.jpg)
Which problem is to be solved?
• Single inheritance• Simple but not
expressive enough
• Leads to• Code duplication• Inappropriate
hierarchies
Vertebrates
Dinosaurs Mammal
fly()
Bat
fly()
Archaeoptrix
![Page 6: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/6.jpg)
Traits
• Set of methods• Possibly composed of other Traits• Composition is unordered• Composition operations
– Sum– Exclusion– Aliasing
• Can be flattened away
MyClass
m2()
TConflict
m1()m2()
TTraitfoo()
TFoo
bar()
TBar
![Page 7: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/7.jpg)
Whiteboarda()
Base
a()
A
^'B'
^'A' a()
T
Com
pile
d Re
sult
Code
Str
uctu
re
a()
Base
a()
A
^'B'
^'A'
^'T'
a()
Base
A
^'B'
a()
T
Com
pile
d Re
sult
Code
Str
uctu
re
a()
Base
a()
A
^'B'
^'T'
^'T'
a()
Base
A
^'B'
a()
T2
Com
pile
d Re
sult
Code
Str
uctu
re
a()
Base
a()
A
^'B'
^'B'
^'T2'
a()
T1
^'T1'
a()
Base
A
^'B'
a()
T2
Com
pile
d Re
sult
Code
Str
uctu
re
a()
Base
a()
A
^'B'
^'T1'
^'T2'
a()
T1
^'T1'
!a
a()
Base
A
^'B'
a()
T2
Com
pile
d Re
sult
Code
Str
uctu
re
a()
Base
a()b()
A
^'B'
^'B'^'T2'
^'T2'
a()
T1
^'T1'
b→a
![Page 8: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/8.jpg)
TRAITS APPLIEDCase Studies Illustrating Benefits of Traits
![Page 9: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/9.jpg)
Use Case for Traits in a PHP Library
• ezcReflection enhances PHP Reflection API– Adds XDoclet like annotations for PHP– Enhances type hinting using PHPDoc type
annotations
• Illustrates solution for code duplication problems «interface»
Reflector
ReflectionProperty ReflectionClass
ReflectionObject
ReflectionFunction
ReflectionMethod
ReflectionParameter
![Page 10: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/10.jpg)
Refactoring ezcReflection with Traits
original with Traitsgreen parts are extracted to the Traits on the right-hand side,
and will be removed from the classes
![Page 11: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/11.jpg)
Case Study: java.io [6]
• Mature code base, in wide use• java.io Library from Java 1.4.2• 79 classes, 25000 lines of code• Results
– 12 classes changed– 30 duplicated methods removed– 14 Traits introduces
![Page 12: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/12.jpg)
Case Study:Smalltalk Collection Classes [10]
• Problem: code reuse vs. conceptual categorization– Unnecessary inheritance– Code duplication– Methods to high in hierarchy– Conceptual shortcomings
• Results for core collection classes– 23 classes changed,
12% less code, 19.4% less methods
![Page 13: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/13.jpg)
Case Study:Smalltalk Collection Classes [10]
• Lessons Learned– Traits simplify refactoring– Very fine-grained traits– Tools are important– Traits enable to defer design of class hierarchy
• Improvements– Uniformity, understandability, reuse
![Page 14: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/14.jpg)
Check for Limitations
• Code duplication• Inappropriate hierarchies• Diamond problem• Fragile hierarchies• Lack of composition control
![Page 15: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/15.jpg)
TRAITS FOR PHPTrait versus Graft
![Page 16: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/16.jpg)
What is a Graft?
• Stateful, non-breakable entity of reuse– Derived from the Traits notion– Proposed with RFC: Non Breakable Traits for PHP
• Why Graft?
From The Grafter's Handbook by R.J. Garner
![Page 17: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/17.jpg)
A Comparison
Traits• Purpose, reuse of
– behavior
• Features– Stateless– Flattenable– Explicit conflict resolution– Highly composable
• Notion– Language-base
Copy and Paste
Grafts• Purpose, reuse of
– modules smaller then classes
• Features– Stateful– Hides complexity– Conflict avoidiance– Strong encapsulation
• Notion– Delegation with self-
impersonation
![Page 18: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/18.jpg)
Traits Syntax and Semanticstrait A { public function small() { echo 'a'; } public function big() { echo 'A'; }
abstract public function doBFoo();
public function doAFoo() { echo 'AFoo uses BFoo: '; $this->doBFoo(); }}
trait B { public function small() { echo 'b'; } public function big() { echo 'B'; }
public function doBFoo() { echo 'B-FOO'; }}
class Talker { use A, B { B::small instead A::small, A::big instead B::big, B::big as talk }}
$talker = new Talker();$talker->small(); // b $talker->big(); // A$talker->talk(); // B
$talker->doAFoo(); // AFoo uses // BFoo: B-FOO
$talker->doBFoo(); // B-FOO
![Page 19: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/19.jpg)
Grafts Syntax and Semanticsgraft Counter { private $cnt = 0; public function inc() { $this->cnt++; } public function reset() { $this->cnt = -1; $this->inc(); }}graft DB { private $db; public function connect() { $this->db = new FooDB('param'); } public function reset() { $this->db->flush(); $this->db = null; } public function doFoo(){echo
'foo';}}
class MyPage { include Counter { public incCnt() as inc(); public resetCnt() as reset(); } include DB { public connect(); public reset(); } public function inc() { /* next page */ }}
$page = new MyPage();$page->connect();$page->incCnt(); //($Countercnt ==
1)$page->resetCnt(); // $Countercnt =
0;$page->inc(); // goto next page$page->doFoo(); // FATAL ERROR
![Page 20: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/20.jpg)
My Mental Image
Traits Grafts
MyClass
TraitA
TraitBMyClass
MyClass
GraftA
GraftBMyClass
![Page 21: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/21.jpg)
ROUNDUP AND CONCLUSIONTrait Support, Discussion, Resume
![Page 22: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/22.jpg)
Environments Supporting Traits• Squeak: In standard distribution since 3.9 [11]
Trait named: #TDrawing uses: {}draw
ˆself drawOn: World canvas in: boundsbounds
self requirement
Object subclass: #CircleinstanceVariableNames: ’’traits: { TDrawing }...
![Page 23: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/23.jpg)
Environments Supporting Traits• Scala: static-typed language running on JVM [7]
trait TDrawing {def draw() : Any =
drawOn(World.canvas(), bounds)def bounds() : Any
}
class Circle extends TDrawing {...
}
![Page 24: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/24.jpg)
Environments Supporting Traits• Perl6: Flavor of Traits with states called Roles
[12]
role TDrawing {method draw() { return drawOn(World.canvas(), bounds);}
}
class Circle does TDrawing {...
}
![Page 25: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/25.jpg)
Environments Supporting Traits• Approaches for plain Java
– Using AspectJ [2]– Utilizing an Eclipse Plugin [8]
![Page 26: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/26.jpg)
Environments Supporting Traits• Approaches for plain Java
– Using AspectJ [2]– Utilizing an Eclipse Plugin [8]
![Page 27: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/27.jpg)
Environments Supporting Traits• C#: Prototype Implementation for Rotor [9]
trait TDrawing {public Object draw() { return drawOn(World.canvas(), this.bounds);}requires { public Object bounds(); }
}
class Circle {uses { TDrawing }...
}
![Page 28: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/28.jpg)
Resume
• Traits are a appropriate language construct to:– Achive conceptual consistent class hierarchies– Avoid code duplication– Enhance class compositon capabilites
• Available for different languages• Two different flavors proposed for PHP
![Page 29: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/29.jpg)
Request for Comments
Traits for PHPVersion: 1.5Date: 2008-03-06Author: Stefan MarrWiki: http://wiki.php.net/rfc/traitsreST: http://www.stefan-marr.de/rfc-traits-for-php.txtHTML: http://www.stefan-marr.de/artikel/rfc-traits-for-php.html
Non Breakable Traits for PHPVersion: 1.0Date: 2008-02-29Author: Joshua ThompsonWiki: http://wiki.php.net/rfc/nonbreakabletraits
![Page 30: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/30.jpg)
Basic Literature
Main Article 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 Formalization, 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 Unanticipated Name Clashes in Traits, SIGPLAN Not., 42 (2007), pp. 171–190.
![Page 31: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/31.jpg)
Literature[1]A. BERGEL, S. DUCASSE, O. NIERSTRASZ, AND R. WUYTS, Stateful Traits and their
Formalization, 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 Unanticipated Name Clashes in Traits, SIGPLAN Not., 42 (2007), pp. 171–190.
[5]S. MARR AND F. MENGE, ezcReflection, SVN Repository, eZ Components, January 2008. http://svn.ez.no/svn/ezcomponents/experimental/Reflection.
[6]E. R. MURPHY-HILL, P. J. QUITSLUND, AND A. P. BLACK, Removing Duplication 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 applications, New York, NY, USA, 2005, ACM, pp. 282–291.
![Page 32: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/32.jpg)
Literature[7] PROGRAMMING METHODS LABORATORY, Traits for Scala, Programming
Language, Ecole Polytechnique Fédérale de Lausanne, 2006. http://www.scala-lang.org/intro/traits.html.
[8] P. J. QUITSLUND, E. R. MURPHY-HILL, AND A. P. BLACK, Supporting 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 Microsoft 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. http://www.iam.unibe.ch/ scg/Research/Traits/index.html.
[12] L. WALL, Apocalypse 12: Class Composition with Roles, tech. report, The Perl Foundation, 2004. http://www.perl.com/pub/a/2004/04/16/a12.html?page=11.
![Page 33: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/33.jpg)
Any questions?Discussion•What du you think?•Traits vs. Grafts
![Page 34: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/34.jpg)
TRAITS ENHANCEDUser-Changeable Visibility and Stateful Traits
![Page 35: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/35.jpg)
User-Changeable Visibility
• Add flexibility to conflict handling• Introduces “Trait private” methods• Methods visibility changeable on composition
+move()
Penguin
+swim()+getExtremity() : Wings
Swimmer
+walk()+getExtremity() : Legs
Walker
hide getExtremity
![Page 36: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/36.jpg)
Stateless Traits
• Problem: stateless Traits are incomplete, not self-contained units of reuse– Required state accessors bloat the interfaces– Encapsulation is violated by public accessors– Reuse, composition is hampered– State access introduces fragility
![Page 37: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/37.jpg)
Stateless Traits
getNuts()setNuts()
nuts
Scrat
Squirrel Rat
collect()getNuts()setNuts()eat()
NutEater
getNuts()setNuts()
getNuts()setNuts()
nuts
Hamster
getNuts()setNuts()
nuts
WildBoar
![Page 38: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/38.jpg)
Stateful Traits
• Solution requirements:– Preserver faltening property– Minimal and general approach– Language independed
• Solution– State is private to the Traits scope– Client can request access under new private name– Client can merge state
![Page 39: Traits: A New Language Feature for PHP?](https://reader034.vdocument.in/reader034/viewer/2022042623/547719f4b4af9ff1248b4cbc/html5/thumbnails/39.jpg)
Stateful Traits
Scrat
collect()eat()
nuts
NutEater
Hamster
WildBoar
Squirrel Rat