component library (can be used as a full framework). • FLOSS (Modified BSD License) with Copyright License Agreement. • Corporate Backed by Zend Enterprises. • Chief Architect is Matthew Weier O‘Phinney.
(fun to learn). • Natural upgrade path for ZF1. • Uses new PHP 5.3 features for better performance & design (newer fancy architecture). • Better quality (built from experience with ZF1).
you need to know about PHP 5.3 to understand some of the concepts in Zend Framework 2. These are: • Namespacing. • Anonymous functions and closures. • Late Static Binding.
as of 5.3. • We’ve (Community) been doing it in (sort of) using underscores. • Just an organizational tool, denoted be backslashes. • Adds importing and aliasing (think .NET and Java). “use x” and “use x as y” <?php namespace Zend\Session\Storage ; use ArrayObject , Zend\Session\Storage , Zend\Session\ Exception ; class ArrayStorage extends ArrayObject implements Storage { / * . . . * / }
Closures are the same as anonymous functions but can have access to parent scope elements. <?php $var = ’ t e s t ’ ; / / This i s a closure , note the use statement $func = function ( ) use ( $var ) { echo $var ; }; $func ( ) ;
in static method. • Adds several functions and constants. • So far I haven’t seen this used in ZF2 yet, but I wouldn’t be surprised if it makes it into things dealing with modeling and the database.
s t a t i c who ( ) { echo CLASS ; } public s t a t i c t e s t ( ) { s e l f : : who ( ) ; s t a t i c : : who ( ) ; } } class B extends A { public s t a t i c who ( ) { echo CLASS ; } }
Framework? • Works on Registered “Namespaces” such as Zend. • Takes classname and replaces with / in the classname and adds .php to the end. • Checks the include path for the filename it comes up with, if it exists include it. • Similar to other “1st generation” framework autoloaders. • Can add other “Namespaces”.
d / Loader / StandardAutoloader . p h p ; $loader = new Zend\Loader\ StandardAutoloader ( array ( f a l l b a c k a u t o l o a d e r => true , ) ) ; $loader−>r e g i s t e r ( ) ;
autoloaders (Classmap, PSR0, prefix/namespace specific autoloading) • Can chain “fallback” autoloaders • Higher performance due to direct requests • ZF2 no longer uses require once statements in the source files
n d / Loader / StandardAutoloader . p h p ; $loader = new Zend\Loader\ StandardAutoloader ( ) ; $loader−>registerNamespace ( M y , DIR . / . . / l i b r a r y / M y ) −>r e g i s t e r P r e f i x ( P h l y , DIR . / . . / l i b r a r y / P h l y ) ; $loader−>r e g i s t e r ( ) ;
y \Foo\ B a r => DIR . / Foo / Bar . p h p , ) ; require once Z e n d / Loader / ClassMapAutoloader . p h p ; $loader = new Zend\Loader\ClassMapAutoloader ( ) ; $loader−>registerAutoloadMap ( DIR . / . . / l i b r a r y / . classmap . p h p ) ; $loader−>r e g i s t e r ( ) ;
need to create classmaps, • ZF2 has a tool to generate these for you. • Shows significant performance improvement over “include path” autoloaders. • According to the Supreme Allied Commander (Matthew Weier O‘Phiney) Class-Maps show a 25% speed improvement over ZF1 Autoloader and a 60% to 85% improvement with a opcode cache enabled.
you to quickly assemble your autoloading strategy and implement it. <?php require once Z e n d / Loader / AutoloaderFactory . p h p ; use Zend\Loader\ AutoloaderFactory ; AutoloaderFactory : : factory ( array ( Z e n d \Loader\ C l a s s M a p A u t o l o a d e r => array ( DIR . / . . / l i b r a r y / . classmap . p h p , DIR . / . . / a p p l i c a t i o n / . classmap . p h p , ) , Z e n d \Loader\ S t a n d a r d A u t o l o a d e r => array ( n a m e s p a c e s => array ( Z e n d => DIR . / . . / l i b r a r y / Z e n d , ) , f a l l b a c k a u t o l o a d e r => true , ) , ) ) ;
as of right now we still do require/include once statements, mostly for performance reasons. • We’ve tested ZF2 Class Map autoloading with the PSR0 Fallback using APC and saw 20% performance increase (approx 40ms on the front-end.)
as of right now we still do require/include once statements, mostly for performance reasons. • We’ve tested ZF2 Class Map autoloading with the PSR0 Fallback using APC and saw 20% performance increase (approx 40ms on the front-end.) • Not in production yet but it is on our roadmap (we have a large code base and it’ll take some time to adapt it all and test it).
has already taken the liberty to package the ZF2 autoloader as a PHP 5.2 compliant package, you can get it http://bit.ly/zf2autoloadfor52 • I’ve ported this into the ZendX namespace in a PHP 5.3 compliant package, find me later and I’ll get you a copy
Construction / Design Pattern, previously discussed (last month actually). • Pattern outlines that all Dependencies should be passed into an object rather than “retrieved” by said object. • Allows for more flexibility and better testing. • In this case it’s a Dependency Injection Container. • Again it’s just a fancy registry.
using Dependency Injection with it’s Controllers and for provided Services. • Most of the library components do use Dependency Injection as their design pattern. • Zend\Di is a construction helper, everything in ZF2 thus far can run without the DI Container (maybe not the locater).
do injections. • These maps can be in php, ini, or xml format (Zend\Config) • Supports Setter Injection & Lazy Initialization instead of just Constructor Injection. • May support Dynamic Resolution of Dependencies in the future (Reflection) • The container, last I tested, was a bit heavy but I know improvements were being made.
e f i n i t i o n , Zend\Di\Reference , Zend\Di\ DependencyInjector as DI ; $db = new D e f i n i t i o n ( ’My\Db\Adapter\ S q l i t e ’ ) ; $db−>setParam ( ’name ’ , DIR . ’ / . . / data / db / users . db ’ ) ; $mapper = new D e f i n i t i o n ( ’My\Mapper\Db ’ ) ; $mapper−>addMethodCall ( ’ setAdapter ’ , array (new Reference ( ’ db ’ ) ) ) ; $service = new D e f i n i t i o n ( ’My\Resource\Users ’ ) ; $service−>setParam ( ’ mapper ’ , new Reference ( ’ mapper ’ ) ) ; $di = new DI ; $di−>s e t D e f i n i t i o n s ( array ( ’ db ’=> $db , ’ mapper ’ => $mapper , ’ users ’=> $service ) ) ; $users = $di−>get ( ’ users ’ ) ; / / My\Resource\Users
i n i t i o n , Zend\Di\Reference ; $service = new D e f i n i t i o n ( ’mwop\ Service \Resources ’ ) ; $service−>addMethod ( ’ setResource ’ , array ( new Reference ( ’ resource ’ ) ) ) ; $di−>s e t D e f i n i t i o n ( ’ resources ’ , $service ) ; $resources = $di−>get ( ’ resources ’ ) ;
well with Dependency Injection with it’s components. • The majority of configuration options require arrays and there is some differing structure for these arrays. • Controllers require an inherited constructor (which is why most of the initialization is done in init() ) • Still works good for Service Containers and Data Models
Aspect Oriented Programing principles in PHP, (crosscutting concerns). • A combination of subject/observer pattern, PubSubHub, and Signal Slots? (wtf is all this?) • A better way of notifying other parts of the system that an event is about to occur/has occured with filters so that we can interact on the event.
events without modifying/overwriting framework code. • Allows you to create hook points for other parts of the system to interact with at runtime. • It’s a major evolutionary step to Programing Philosophy (ask me more on Aspect Oriented Programing later if you’re interested)
t d l i b \ CallbackHandler ; i n t e r f a c e EventCollection { public function t r i g g e r ( $event , $context , $argv = array ( ) ) ; public function t r i g g e r U n t i l ( $event , $context , $argv , $callback ) ; public function attach ( $event , $callback , $ p r i o r i t y = 1) ; public function detach ( CallbackHandler $handle ) ; public function getEvents ( ) ; public function getHandlers ( $event ) ; public function clearHandlers ( $event ) ; }
EventManager ( ) ; $events−>t r i g g e r ( $eventName , $object , $params ) ; / * Where : * − $eventName i s the name of the event ; usually the current * method name * − $object i s the object t r i g g e r i n g the event * − $params are the parameters the handler might need to access , * usually the method arguments * /
dependencies actually becomes really easy; use statements at the beginning of the file will usually tell you what components you need from outside of that namespace.
replace the namespace separator with the ZF1 but be sure to find and replace aliases. • It may be worth renaming the namespace from Zend to ZendX if using Zend Framework 1 to keep from mixing up components. • Be sure you remove all references to namespace, use, as, and \ from the code, PHP 5.2 will barf on these.
them in ZF1 Projects (with a little work). • ZF2 Components have benefits, performance being one, shininess being two. • Integration ZF2 Components now will make refactoring to use ZF2 later easier. • Why Not? The idea behind Zend Framework is to make reusable components (it’s technically a library not a framework), don’t get stuck on legacy library components just because you can’t upgrade your whole stack.