• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content


Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

New things in php



Presentation from Juan Basso, Technical Architect, about few PHP features to all developers in Technology department. The goal is to keep all developers in synch with all features used by Zumba ...

Presentation from Juan Basso, Technical Architect, about few PHP features to all developers in Technology department. The goal is to keep all developers in synch with all features used by Zumba applications.



Total Views
Views on SlideShare
Embed Views



1 Embed 2

https://twitter.com 2



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    New things in php New things in php Presentation Transcript

    • AUTOLOAD Pros:Dont worry about require/includeLazy load files Cons:Slower than requireRequire some standard in file names
    • AUTOLOAD Example of file organizationlib / PHPUnit / Framework / TestCase.phplib / PHPUnit / Framework / TestSuite.phpautoloader.php
    • AUTOLOAD - HOW TO NOT LOADfnto _atla(cas { ucin _uood$ls) $ieae=srrpae DRCOYSPRTR $ls) flnm t_elc(_, IETR_EAAO, cas; i (ieeit(flnm) { f fl_xss$ieae) rqie$ieae eur flnm; }} Used in PHP 4 Limited to one per request DO NOT USE IT!!!
    • AUTOLOAD - HOW TO LOADfnto ppntlae(cas { ucin hui_odr$ls) i (tps$ls,0 8 !=HUi_){ f sro(cas , ) = PPnt rtr; eun } $ieae=srrpae DRCOYSPRTR $ls) flnm t_elc(_, IETR_EAAO, cas; i (ieeit(flnm) { f fl_xss$ieae) rqie$ieae eur flnm; }}slatla_eitrhui_odr) p_uoodrgse(ppntlae; Available since PHP 5.1.2 Allow multiple registrations per request Can be any callable
    • AUTOLOAD - COMMON PATTERN PEAR - UNDERSCORED CLASSESEvery underscore in the class means the directory separator. Compatible with PHP 4. Ie, P P n t F a e o k T s C s refers to HUi_rmwr_etae PPntFaeokTsCs.h. HUi/rmwr/etaepp
    • AUTOLOAD - COMMON PATTERN PHP STANDARD RECOMMENDATION (PSR-0) Same as PEAR, but also cover namespaced classes.In namespaces, the namespace separator is replaced by directory separator.Ie, C k o t o l r C m o e t A t C m o e t a e C n r l e o p n n u h o p n n referens to aeCnrle/opnn/uhopnn.h. Ck/otolrCmoetAtCmoetpp
    • STANDARD PHP LIBRARY (SPL)Classes, interfaces and functions built for standard problems.Most used: exceptions, iterators, file manipulation, autoload
    • SPL - COUNTABLE Can use c u t o n function in objects.casFoipeet Cutbe{ ls o mlmns onal poetd$tm =ary) rtce ies ra(; pbi fnto ad$tm { ulc ucin d(ie) $hs>tm[ =$tm ti-ies] ie; } pbi fnto cut){ ulc ucin on( rtr cut$hs>tm) eun on(ti-ies; }}$b =nwFo) oj e o(;$b-ada; oj>d(br)$b-adobr) oj>d(foa;$ie=cut$b) / Rsls2 sz on(oj; / eut
    • SPL - TRAVERSABLE Allow f r a h objects. o e c incasFoipeet Taesbe IeaoAgeae{ ls o mlmns rvral, trtrgrgt poetd$ =o rtce a fo; poetd$ =a rtce b br; pbi fnto gttrtr){ ulc ucin eIeao( rtr nwArytrtr$hs; eun e raIeao(ti) }}$b =nwFo) oj e o(;frah(oja $e = $au){ oec $b s ky > vle }/* Sm a: ae s ary ra( = o a > fo, = a b > br )*/
    • SPL - FILE INFO p F l I f give information about specific file. Can be used withSlieno folders and symbolic links too. ty{ r $ie=nwSlienopt/ofl; fl e pFlIf(/aht/ie) $ie>sie) / boeni i i afl fl-iFl(; / ola f t s ie $ie>srtbe) / Cnwiei tefl fl-iWial(; / a rt n h ie $ie>eBsnm(;/ Flnm,wtotfle fl-gtaeae) / ieae ihu odr $ie>eLnTre(;/ Weeteln ge fl-gtikagt) / hr h ik os $ie>eSz(;/ Fl sz fl-gtie) / ie ie $ie>eRaPt(;/ Ra pt o fl fl-gtelah) / el ah f ie }cth(xeto $){ ac Ecpin e eh $-gtesg(;/ Poal afl ntfud co e>eMsae) / rbby ie o on }
    • SPL - DIRECTORY ITERATOR Look for files, folders and symbolic links in specific folder.$i =nwDrcoytrtr_DR_; dr e ietrIeao(_I_)frah(dra $ieno { oec $i s flif) i ($ieno>so(){ f !flif-iDt) cniu; otne } eh $ieno>eFlnm(,"n; co flif-gtieae) "}
    • SPL - EXCEPTIONS There are couple build in exceptions.i (vle>$a | $au <$i){ f $au mx | vle mn trwnwOtfonsxeto(; ho e uOBudEcpin)}sic (vle { wth $au) cs o ae fo: rtr a eun br; dfut eal: trwnwUepceVlexeto(; ho e nxetdauEcpin)}
    • SPLCheck more in http://php.net/spl
    • EXCEPTIONSStop the code at that point and just stop in a c t h a c block. Help to revert changes in case of problems.
    • EXCEPTIONS Example:ty{ r $b>eiTascin) d-bgnrnato(; $b>ur(UDT ..) d-qeyPAE .; $b>ur(ISR ..) d-qeyNET .; $b>ur(UDT ..) / ti trw a ecpin d-qeyPAE .; / hs hos n xeto $b>omt) d-cmi(;}cth(xeto $){ ac Ecpin e $b>olak) d-rlbc(; eh $-gtesg(; co e>eMsae)}
    • EXCEPTIONSDoesnt matter the number of levels, it will stop until the c t h ac.fnto a){trwnwEcpinos) } ucin ( ho e xeto(Op;fnto b){ ucin ( eh hswrs; co Ti ok a) (; eh hsi ntcle co Ti s o ald;}fnto c){ ucin ( eh hswrs; co Ti ok b) (; eh hsi ntcle co Ti s o ald;}ty{ r c) (;}cth(xeto $){eh gta)ecpin } ac Ecpin e co I o ( xeto!;
    • EXCEPTIONS What happen if you not catch the exception? PPFtlerr Ucuh ecpinxetoi .. H aa ro: nagt xeto Ecpin n . Saktae tc rc: # {an .. 0 mi} .Remember it could happen in the middle of a money transaction!
    • EXCEPTIONSExceptions are classes. You can extend and take different actions for each case. ty{ r mto1) ehd(; mto2) ehd(; mto3) ehd(; }cth(naiAgmnEcpin$1 { ac Ivldruetxeto e) eh oemto rcie a ivldagmn co Sm ehd eevd n nai ruet; }cth(agEcpin$2 { ac Rnexeto e) eh os sm rnepolm; co Op, oe ag rbe }cth(xeto $){ ac Ecpin e eh n gnrcecpin; co Ay eei xeto } Catch is tested in order using i s a c o . ntnef
    • EXCEPTIONS S a C i n throws exception!!! opletAlways, I mean always, use t y c t h r / a c when you do SOAP requests. ty{ r $yesuc =nwSaCin(.) cbrore e oplet..; $yesuc-cagCr(41111111 ..; cbrore>hread11111111, .) }cth(opxeto $){ ac SaEcpin e / Tascinfie.D ntpaeteodr / rnato ald o o lc h re }
    • ABSTRACT A class that cant be instanciated directly. Used as base of other classes to have a standard.asrc casFo{ btat ls o }casBretnsFo{ ls a xed o }$a =nwBr) br e a(;/ O / K$o =nwFo) fo e o(;/ PPFtlerr Cno isataeasrc casFo.. / H aa ro: ant ntnit btat ls o .
    • ABSTRACTasrc casUe { btat ls sr poetd$ae rtce nm; pbi fnto nm( {rtr $hs>ae } ulc ucin ae) eun ti-nm; asrc pbi fnto iAlTBy) btat ulc ucin sbeou(;}casCnue etnsUe { ls osmr xed sr pbi fnto iAlTBy){rtr tu;} ulc ucin sbeou( eun re}casAmnetnsUe { ls di xed sr pbi fnto iAlTBy){rtr fle } ulc ucin sbeou( eun as;}casDsrbtretnsUe { ls itiuo xed sr}/ I wl cueftlerrbcued nt / t il as aa ro eas o o/ ipeetiAlTByfnto / mlmn sbeou ucin
    • ABSTRACTYou can make your method expect an abstract class because you know there is a method. casSo { ls hp pbi fnto ii(sr$sr { ulc ucin ntUe ue) i ($sr>sbeou(){ f !ue-iAlTBy) rtr fle eun as; } / .. / . } } $sr=nwCnue(; ue e osmr) $hp=nwSo(; so e hp) $hp>nt$sr; so-ii(ue) vrdm(ue isaco Cnue) / Rtr tu a_up$sr ntnef osmr; / eun re vrdm(ue isaco Ue) / Rtr tu a_up$sr ntnef sr; / eun re
    • INTERFACECant be instanciated or extended.Can use multiple in one class.Can provide abstract methods.Can provide attributes.Cant provide implemented methods.Can check if a class implements using i s a c o . ntnef
    • INTERFACEitraeFo{ nefc o pbi fnto br) ulc ucin a(;}casBa ipeet Fo Cutbe{ ls lh mlmns o, onal pbi fnto br){ ulc ucin a( rtr obr; eun foa } pbi fnto cut){ ulc ucin on( rtr 0 eun ; }}vrdm(e Ba( ipeet Fo;/ Rtr tu a_upnw lh) mlmns o) / eun re
    • FINAL Do not allow override by child classes. Final can be used with classes and methods.fnlcasFo{ ia ls o fnlpbi fnto br){ ia ulc ucin a( }}
    • FINAL What is the different of f n l i a and p i a e rvt?casFo{ ls o fnlpbi fnto ii( { ia ulc ucin nt) } piaefnto do( { rvt ucin rp) }}casBr{ ls a pbi fnto _cntut){ ulc ucin _osrc( $hs>nt) / I i O ti-ii(; / t s K $hs>rp) / I i ntO ti-do(; / t s o K }}
    • TRAITSLazy Copy and Paste! PHP 5.4+
    • TRAITStatFo{ ri o pbi fnto lg$esg){ ulc ucin o(msae Lg:rt(msae; o:wie$esg) } pbi fnto sm$,$){ ulc ucin u(a b rtr $ +$; eun a b } pbi fnto hr( { ulc ucin ee) rtr $hs>ee eun ti-hr; }}casBr{ ls a ueFo s o; poetd$ee rtce hr;}$b =nwBr) oj e a(;$b-foaa) oj>o(Ll;
    • TRAITSCan use multiple traits in the same class.Can change method visibility from the traits.Traits can use traits.Traits can contain a s r c methods, attributes. btat
    • NAMESPACESThe partner library has a class called A d e sThe application also drs. has A d e s d r s class. How do you integrate???
    • NAMESPACESCan you have the same filename in the same directory? No. It happen with PHP classes too. Solution: namespaces!Namespaces separate classes in groups and allow to re-use the same class name in different scope.
    • NAMESPACESnmsaePrnr{ aepc ate casFo{ ls o }}nmsaeMAp{ aepc yp casFo{ ls o }}$ate =nwateo(; prnr e PrnrFo)$yp =nwypo(; map e MApFo)
    • NAMESPACES Classes in the same namespace do not require the prefix.nmsaeFo aepc o;casBr{/ Rmme tecasi oa ls a / eebr h ls s FoBr}casFoa { ls obr pbi fnto _cntut){ ulc ucin _osrc( $hs>a =nwBr) / D ntrqieteo pei ti-br e a(; / o o eur h Fo rfx }}
    • NAMESPACES Class in different namespace require prefix or u e s.nmsaeFo aepc o;casBr{ ls a pbi fnto _cntut){ ulc ucin _osrc( $hs>obr=nwatedrs(; ti-foa e PrnrAdes) }}nmsaeFo aepc o;ueatedrs; s PrnrAdescasBr{ ls a pbi fnto _cntut){ ulc ucin _osrc( $hs>obr=nwAdes) ti-foa e drs(; }}
    • NAMESPACESBe careful to not conflict class from the same namespace with aliases. nmsaeFo aepc o; ueaobr s BrFoa, aaeiea Braeie BrDtTm s aDtTm, aeiea GoaDtTm; DtTm s lblaeie $obr=nwFoa(;/ Isataeaobr foa e obr) / ntnit BrFoa $aeie=nwBraeie) / Isataeaaeie dttm e aDtTm(; / ntnit BrDtTm $aeie=nwGoaDtTm(;/ Isataeaaie dttm e lblaeie) / ntnit DtTm $aeie=nwDtTm(;/ Isataeoaeie dttm e aeie) / ntnit FoDtTm casaisaobr,oaeobr) ls_la(BrFoa FoFkFoa; $obr=nwFkFoa(;/ Tyt isataeoaea, foa e aeobr) / r o ntnit FoFkbr / ta i a aisfraobr / ht s n la o BrFoa
    • NAMESPACESRemember that PHP classes dont define a specific namespace. So they are part of the global namespace. nmsaeFo aepc o; casBr{ ls a pbi fnto _cntut){ ulc ucin _osrc( / Wog-I wl lo frtecasoaeie / rn t il ok o h ls FoDtTm $hs>o =nwDtTm(; ti-nw e aeie) / Cret / orc $hs>o =nwaeie) ti-nw e DtTm(; } }
    • NAMESPACES Be very careful with exceptions too!nmsaeFo aepc o;ty{ r fn1) / Ti mto rtr agoa Ecpin uc(; / hs ehd eun lbl xeto}cth(xeto $){ ac Ecpin e / Ti cd i ntcle / hs oe s o ald}cth(Ecpin$){ ac xeto e / Ti cd i cle / hs oe s ald}
    • NAMESPACESNamespaces are not only for classes. You can do for functions too! nmsaeFo aepc o; fnto sbt(srn,$tr,$n =nl){ ucin usr$tig sat ed ul rtr oehn seil; eun smtig pca } / I wl uetefnto aoe / t il s h ucin bv $a =sbt(vr 0 1) vr usr$a, , 0; / I wl uetePPfnto / t il s h H ucin $a =usr$a,0 1) vr sbt(vr , 0; / I nti tenmsae PPwl uegoa / f o n h aepc, H il s lbl $o =sro(vr a; ps tps$a, br)
    • SESSION OR CACHE?User informations? SessionProduct information? CacheProducts bought by User? CacheCredit card? None!User addresses? Do you really need to store it?
    • SESSION OR CACHE How to decide? When the information...Is strict for the user and can be reused in couple pages: store insession.Is shared between multiple users: store in cache.Is sensitive: do not store.Is big: consider to store.
    • SESSION OR CACHE Why not store big data? It save queries! Yes, it saves database, but remember that session is saved andrestored every request. If you have big data in the session, it will be very slow to get the session in all pages.
    • SESSION - HOW IT WORKS Client side:Client (browser) just store one IDClient (browser) send the ID in all requestsUsually the ID is a cookie, but could be a query/post parameterThe content of the session is not accessible by the browser
    • SESSION - HOW IT WORKS Server side:Server receive the ID from the browser or generate a new IDServer load the session from the storage (database, file, memcache,...)Code read/write the values in the sessionWhen request finish, PHP put the session content on the storageagainWith the content, the expiration date/time is also savedEven if the session is not changed, it must be saved to update theexpiration time
    • CONSOLECan run console applications using PHP similar from web. Provide $ r c a g and $ r v a g global variables. Dont have few attributes from $ S R E . _EVR Finish with codes for the OS. Can create threads/forks. Can catch system events (ie, ctrl+c). Can run for long time. Can receive parameters from the terminal. Can give you headaches.
    • PHPUNITcasMt { ls ah pbi fnto sm$,$){ ulc ucin u(a b rtr $ +$; eun a b }}casMtTs etnsPPntFaeokTsCs { ls ahet xed HUi_rmwr_etae pbi fnto tsSm){ ulc ucin etu( $ah=nwMt(; mt e ah) $hs>setae2 $ah>u(,1) ti-asrSm(, mt-sm1 ); $hs>setae0 $ah>u(,-); ti-asrSm(, mt-sm1 1) }}
    • PHPUNIT - RESULTSjbsozma/m$ppntMtTs.h ras@ub:tp hui ahetppPPnt370C b SbsinBrmn. HUi ..R2 y eata egan.Tm:0scns Mmr:27M ie eod, eoy .5bO ( ts,2asrin) K 1 et setos
    • PHPUNIT - USUAL ASSERTSa s r S m - Type and value identicals, same of = = setae =a s r E u l - Same value, same of = setqas =a s r T u and a s r F l e Check boolean values setre setas -a s r N l - Check null values setula s r E p y Check empty variables, same of e p y ) setmt - mt(a s r I s a c O - Check if the class (or parent class) matches. setntnefSame of i s a c o ntnefAll assert above has the not version, ie a s r N t a e setoSm
    • PHPUNIT - PARAMS AND MESSAGES Param order matter! Expected values first.$hs>setae$eut 5;/ Wog ti-asrSm(rsl, ) / rn$hs>setae5 $eut;/ Cret ti-asrSm(, rsl) / orc You could put error messages to enhance.$hs>setae2 $eut wsepcigmr fo yu) ti-asrSm(, rsl, I a xetn oe rm o;1 MtTs:tsSm ) ahet:etuIwsepcigmr fo yu a xetn oe rm oFie asrigta 2 i ietclt 2 ald setn ht 3 s dnia o .
    • PHPUNIT - ANNOTATION/EXCEPTIONS pbi fnto wos){ ulc ucin op( trwUepceVlexeto(Ntago ie; ho nxetdauEcpino od da) } pbi fnto tsWoslentv( { ulc ucin etopAtraie) $hs>eEpceEcpinnxetdauEcpin) ti-stxetdxeto(UepceVlexeto; $hs>op(; ti-wos) } /* * *@xetdxeto UepceVlexeto epceEcpin nxetdauEcpin */ pbi fnto tsWos){ ulc ucin etop( $hs>op(; ti-wos) }REMEMBER: PHPUnit is PHP! Anything after the exception is NOT called! Dont put 2 tests in the same test method.
    • PHPUNIT - DATA PROVIDERS DRY!/* * *@aarvdrsmaa dtPoie uDt */pbi fnto tsSm$,$,$xetd { ulc ucin etu(a b epce) $hs>setre$xetd $hs>ah>u(a $); ti-asrTu(epce, ti-mt-sm$, b)}pbi fnto smaa){ ulc ucin uDt( rtr ary eun ra( ary1 1 2, ra(, , ) ary1 -,0, ra(, 1 ) ary100 1,103, ra(00, 3 01) );}
    • PHPUNIT - MOCK Check values or ignore behavior or bothcasA{ ls pbi fnio rn$k { ulc uctn u(o) i (o){ f $k $hs>mi(ue@etcm) ti-ealsrts.o; rtr tu; eun re } rtr fle eun as; } poetdfnto eal$o { rtce ucin mi(t) rtr mi(t,ti o K) eun al$o I s k, O; }}
    • PHPUNIT - MOCKcasAetetnsPPntFaeokTsCs { ls Ts xed HUi_rmwr_etae pbi fnto tsRn){ ulc ucin etu( $ok=$hs>eMc(A,arymi) mc ti-gtok ra(eal); $ok>xet(ti-a() mc-epcs$hs>t0) -mto(eal) >ehdmi -wt(ti-eul(ue@etcm) >ih$hs>qassrts.o) -wl(ti-rtrVletu); >il$hs>eunau(re) $hs>setre$ok>u(re) ti-asrTu(mc-rntu); $ok>xet(ti-nvr) mc-epcs$hs>ee() -mto(eal) >ehdmi; $hs>setas(mc-rnfle) ti-asrFle$ok>u(as); }}
    • PERFORMANCENetwork is your enemy!Cache can be fast, but involves networkRetrieve big content from cache/database is better than multiplesmallDo the table(s) the indexes necessary for your query?Big session, big delay, big headachesWeb servers are scalable, use it!Use more CPU and memory instead heavy requests to database
    • FASTCGI PROCESS MANAGER (FPM)Run PHP as daemon. Generic for any web server (apache, nginx, ...). One web server can use multiple FPM servers (remotely). Faster and safer than regular CGI. Best option for non-apache solutions.