ebook img

ultimate guide to object oriented php for wordpress developers PDF

104 Pages·2017·2.9 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview ultimate guide to object oriented php for wordpress developers

ULTIMATE GUIDE TO OBJECT ORIENTED PHP FOR WORDPRESS DEVELOPERS LEVELING UP AS WORDPRESS DEVELOPER WITH OBJECT-ORIENTED PHP BY JOSH POLLOCK WHAT’S INSIDE INTRODUCTION 5 PHP IN A REST API WORLD 5 PHP 7 — GUEST CHAPTER BY TOM EWER 7 HOW PHP TOK OVER THE WEB 8 PHP 7 FINALY HEAVES INTO VIEW 8 WORDPRES IS ABOUT TO GET WINGS 9 THERE ARE TWO KEY POINTS TO EMPHASIZE HERE 10 WHY DEVELOPERS ARE HOLDING FIRE ON SWITCHING 11 ARE YOU GOING TO USE PHP 7 IN PRODUCTION? 1 WHAT’S NEXT? 12 PHP FUNDAMENTALS 13 VARIABLES AND CONSTANTS 14 DATA TYPES 15 FUNCTIONS AND SCOPE 17 OBJECT-ORIENTED PHP 19 BEFORE WE BEGIN 20 METHODS VS. FUNCTIONS 20 PROPERTIES VS. VARIABLES 2 USING HOKS IN CLASES 2 MORE FUN WITH _CONSTRUCT() 23 WP_QUERY: THE OBJECT-ORIENTED PHP 24 A LITLE BACKGROUND 25 WP_QUERY 26 MAKING YOUR OWN 27 WORDPRES AS A GATEWAY TO OBJECT-ORIENTED 27 VISIBILITY IN OBJECT-ORIENTED PHP 28 ENCAPSULATION AND SCOPE 29 CLASES VS. OBJECTS 30 THE THRE LEVELS OF VISIBILITY 30 RULES OF PROPERTY VISIBILITY 31 RULES OF METHOD VISIBILITY 3 WHY VISIBILITY MATERS 35 A FEW LAST WORDS 36 CLAS INHERITANCE IN OBJECT-ORIENTED PHP 37 CLAS INHERITANCE 38 EXTENDING CLASES AND OVERIDING 38 ABSTRACT CLASES 39 LES CODE, BETER CODE 41 HOW TO USE ASYNCHRONOUS PHP IN WORDPRES 42 HOW IT WORKS 43 THE PROBLEM: AL AT ONCE OR NOTHING 4 SETING IT UP 4 WIRING IT UP 47 MORE ASYNCHRONOUS 46 REST APIS AND PHP 48 DESIGNING THE SYSTEM 49 PUTING IT TOGETHER 5 STARTING IT UP 57 STARTING WITH A SYSTEM 59 PHP MAGIC METHODS 60 CONSTRUCTOR 61 SETING HOKS IN THE CONSTRUCTOR 61 MAGIC SETERS AND GETERS 62 CONVERTING OBJECTS TO STRINGS 6 USE MAGIC, LEARN MORE MAGIC 68 NAMESPACES 69 NAMESPACING YOUR CLAS 70 NESTING NAMESPACES 73 ALIASING NAMESPACES 74 NAMESPACE AL THE THINGS! 75 IMPROVING DEVELOPMENT WORKFLOW WITH COMPOSER 76 WHEN AND WHERE TO USE COMPOSER 7 INSTALING COMPOSER 79 WRITING A COMPOSER FILE 79 WAVING THE BATON 82 USING A CLAS AUTOLOADER TO IMPROVE WORDPRES DEVELOPMENT 83 WHAT IS A CLAS AUTOLOADER? 84 CHOSING A STANDARD 84 NOT CHOSING A STANDARD 85 USING THE COMPOSER AUTOLOADER 85 USING A PSR-4 AUTOLOADER 86 THAT’S AL IT TAKES 87 PHP DESIGN PATERNS FOR WORDPRES DEVELOPERS 8 EVENT DRIVEN VS. MODEL VIEW CONTROLER 89 USEFUL PHP DESIGN PATERNS 90 THE SINGLETON 90 WORDPRES CORE GLOBALS 94 WHY? 95 IT’S TIME TO LEVEL UP YOUR PHP SKILS 96 READ MORE SOURCE 97 MISUSED PATERNS 98 COMPOSER AND CROS-POLINATION 9 YOUR TURN 10 CONCLUSION 10 KOBE s repole veD se rPd ro W ro F PHP detneirO- tcejbO o T ediuG etamit lU INTROUD TC ION Tere are a lot of reasons why WordPress is so popular, but one is by far the fexibility and The first step ease of use of the two languages it is written in: PHP and JavaScript. Both languages have a in leveling up fairly low barrier to entry. But, while it is easy to get started, that does not mean it is always easy to learn. More advanced skills are required to create performant, maintainable, reusable your skills as and testable code. a developer Te frst step in leveling up your skills as a developer is learning object-oriented programming for PHP or OOP. is learning OOP is about more than using classes in your code. It’s about creating code that is less object-oriented focused on a specifc action and more focused on objects — small, reusable containers for data and functionality. programming PHP is the most popular programming language in the world — powering 84 percent of all for PHP or OOP. websites. Te server-side scripting language is known for its ability to create dynamic websites and for its use as a general-purpose programming language. PHP is open source, which combined with its ubiquity and capabilities, make it a perfect match for WordPress, the CMS that now powers more than 25 percent of the internet. Although WordPress users don’t need to learn PHP to manage their WordPress-powered websites, if you’re a plugin or theme developer, or just want to modify the default behavior of your site, you will need to have a basic understanding of PHP. There are two types of PHP: OO PHP vs. Procedural. Tis ebook will equip you with the knowledge and skills you need to get started with object- oriented PHP as a WordPress developer, including: • PHP Fundamentals • Visibility and inheritance • PHP 7 • WP_Query • Magic methods And more PHP IN A REST API WORLD In the era of the API-driven JavaScript interface, becoming the norm for WordPress development, PHP is more important than ever for WordPress users. Tis may sound strange, but all of these cool interfaces require a well built server-side application to power that application. Tat server-side code, written in PHP, will require a PHP developer that is well- versed in WordPress’ inner workings. 5 WP ENGINE | TORQUE KOBE s repole veD se rPd ro W ro F PHP detneirO- tcejbO o T ediuG etamit lU gvni o M r ,d o rf aw WPdro se r ,e r oc sgu ni l p dn a y b a em setme h w i l l ene d ot od a re tb te bj o f o gnil p u ed oc gol ci dn a RC U D mof r t eh . y l a p s id e� ems a lel-w v ol sme t sys w i l l ene d ot s re ev tb ho t eh a n lt oi t idar WPdro sse r nimda dn a etme h re ,secaft ni sa l e lw sa RETS PA I .st ni p o d ne New plugins have an advantage in this department, but it requires discipline and more work to make a plugin that meets the requirements I listed above. Adapting an older plugin to meet these demands without breaking backward compatibility is more challenging. I f er ’ u o y a WPdro se r p reol ed v e dt ic xe t ub ao t eh e ru t f u f o WPdro se r ne vrd i y b PA I- e dre w p o tSav p rc a i J re ,secaft ni t neh uo y u dl o h s r el na e r o m .tSav p rc a i J dnA se y o gh niso c a tSav p rc a i J kr oe f m w ar ot r el na — I dnem e oc r SJrug l an A — wi l l p le h hcnu l a u o y ot ni tSav p rc a i J .t ne m p ol ed v tso M , yt l r p n t o a mi u o y u dl o h s eagh l c nel s flru e o y ot etw r i re tb te PPH dn a p ol ed v a gn o t rs p se e c r t r o f t eh ntoi s ar pe a f o srec n n oc dn a t eh g elnis t yiil b is n p se o r . el p r ic ni p 6 WP ENGINE | TORQUE KOBE s repole veD se rPd ro W ro F PHP detneirO- tcejbO o T ediuG etamit lU PHP 7 Guest Chapter by Tom Ewer 7 WP ENGINE | TORQUE KOBE s repole veD se rPd ro W ro F PHP detneirO- tcejbO o T ediuG etamit lU WPdro sse r sah e dy l a p a yle gu h t ngis c�in a el o r ni gni p l e h PPH reu q n oc t eh b e w re v o t eh tsl a In this 21 .sre a y Wih el WPdro sse r sah w ezih d gu ho t rh ev is secu s sn ois re v t a na genis a rc ni- re e v et ar gru ni d t ta h ,te mi t eh egug a n l a t ta h t is l l sre w p o t eh t yr ij o a m f o t eh r mot f l a p sah e ni dame r chapter, ylebid rc ni el bt s a b dnih e t eh s .se nec gi B se g n a h c e r a a yn�l l ni t eh gn� o t hwi t eh r av r a li f o PPH 7 , , re vw o h dn a rj o a m WPdro sse r we’ll take a stsoh hcu s sa WP Egenin er a ye da a rl gkni ci t eh se t ri fo t eh tse t l a el s e a er dn a gt ni te g ye da r ot f u yll r p top u s t i ro f t rie h s .s u re look at the I n t sih , re t p a h c ’ l l e w t ea k a ol ko t a t eh t ne m p ol ed v f o PPH 7 ot ,e t d a t wa h t eh rjo a m se g n a h c ,e r a t wa h t e h y nema r o f WPdro se r s ,su re dn a redis n oc w te reh u o y u dl o h s b e t nih gkni f o development a gmkni t eh hc t ws i ot t eh e n w n ois re v t gi t h ars t u o f o t eh . e t ag s ’ L te e et sgt nih p u t hwi a r fei b pt r i w n od r o me m y . e n l a of PHP 7 HOW PHP TOOK OVER THE WEB to date. PPH s ’ t nec ru r se ecne r p inm o si taso ml t nea k r o f e dt n g ar t se h e ,sy d a t u b e tre h saw re v y t elt il ot tsegu s g t ta h t i u dl o w og n o ot et a nim od t eh be w w neh t i saw ts r� el db b oc te rehgo t y b R su ms a r f od L re kbca ni .491 I n y n am ,sy aw PPH s ’ rs i e ot t eh p o t sah b e ne a hp mut r i f o o go ,d e n d oih s af-dl o wbl eo e s a g er re v o t car sb ta t ci a m go ar r p .srec n n oc I n ttsar n oc ot p gte ni m oc sn t oi u s l o hcu s sa av a J dn a P ,lre t eh egug a n l a saw r dot rf gi aw t h ars gu hone ot t car t t a na ecneidu a e n w ot t eh , be w dn a el p mis gu hone n o t eh s re re v edis ot k yciu l q ebm e oc a d r d an t s a t s a l ni l ntoi p o t a stso h .wedi dl r o w P tu , y l p mis PPH el db a ne a ntoi are ne g f o oc sred ot tsu j te g t i . e n od I st ylr e a nt oi p oda y b a tso h f o rul a p p o SMC sgre ni � o s e a el d t eh ed ,a l t hwi WPdro sse r b gnie y b raf t eh tso m t ngis c�in a f o t .me h e� PPH x.5 s re sei gn ar p s ot ni efil ni ,402 dn a fi e r ’ u o y r gninu WPdro se r , y o t d a e r ’ u o y taso ml rec yni t l a r gninu a r onim n ois re v f o t s ih red nu t eh o h o d sa ew p s e a .k e� x.5 s re sei sah s re e dv PPH le lw re v o ,te mi t u b 21 sre a y si a gn ol t emi b e nete w rj o a m .s n ois re v S ren o o r o , re t l a a eg n a h c saw dnub o ot . e m oc PHP 7 FINALLY HEAVES INTO VIEW erB o fe ew te g ot ni t eh t ytg-y r t i in f o PPH ,7 s ’ tel te g ems o t i a lne t p o gnim a n nfn ois u oc t u o f o t eh . y aw e� tsl a el bt s a el s e a er f o PPH saw PPH 6.5 ni 4102 , s o t a t sih ,e tg s a u o y t gim h l e lw b e gred n ni o w t wa h p e ne dp a h ot PPH .6 T o tcu a gn ol r o t s y r ,to h s e tre h saw a sue v oi r p t p me t t a t a a en w rjo a m n ois re v gnis u t eh em a n PPH 6 mof r 502 ot 0102 t ta h re n v f u yl l t o g � o t eh ,dnu o g r dn a ot di o v a gyni du m t eh ,s re t aw t eh nedois ic saw u a ylt l ne v eda m ot og t gi t h ars m of r t eh x.5 s re sei ot PPH .7 PPH 7 sah b e ne red nu eca v t i t ne m p ol e ed v ecnis ,4102 dn a saw ic� a ylol el s e a e dr ni Db e remec .5102 I st t ne m p ol ed v e r dvr a i t a na gtnise re t ni t emi ni t eh w redi PPH ,dlr o w sa e n w se vt i t i a ini hcu s sa s ’ b eca o k o F p o Hp i H uri a lt V e nih ca M ere w yls u e n o t a u l mis gnid n pxe a t wa h saw yls ue v oi r p t gu h t o h el bp is so t hwi t eh . e g ug a n l a 8 WP ENGINE | TORQUE KOBE s repole veD se rPd ro W ro F PHP detneirO- tcejbO o T ediuG etamit lU PPH 7 sp ih s t hwi a tso h f o f- yrd pnei l re ol ed v .se ru t e a f d A ni t eh caf t t ta h s ’ t i b e ne re v o a ed c eda ecnis t eh tsl a rj o a m el s e ,a e r dn a e tre h saw yl b d a n t s a red nu a tol f o t ne me t ic xe dn a nt oi p a t ici n a gel nida p u ot PPH 7 uaca ylt l gnid n l a – dn a dn l a t i yul d did ! s ’ L te pe t s gu ho t rh t eh ni a m st ni p o t ta h ev a h b e ne gts ni te ’ ps re ol e ed v u s l sep gnicar ecnis t :ne h • It’s a true major release. A rjo a m el s e a er si yle c �e v t i a nel ac ,e t l a s dn a srel ac t eh sked c r o f rj o a m t i a yl lne t p( o e a )gkr ni b .se g n a h c Wt hi t eh t nu o m a f o rc �u t ta h PPH sah e dtre h ag re v o t eh ,s r e a y t sih si t lecnel xe e n sw r o f a l l rec. e n n d oc • Tere’s a brand new engine under the hood. e� Z dne Eg enin II sah b e ne gni od gnil re t s kr o w n o t eh PPH x.5 s re sei re v o t eh ,s r e a y t u b PPH 7 wi l l b e gr� ni n o a l l c syred nil n skt a h ot t eh n gpk nias e n w PPH NG N( txe G)ntoi are ne g eni ne t ta h sre w p o .t i • A host of powerful new language features are available. e� tse t l a n ois re v f o PPH 7 sp ih s t hwi t y nel p f o e n w sn t oi p o r o f p sreol ed v ot ,e r ol pxe gnidu l c ni t py e ,s n t oi ar l ed a c p ecas p ih s ,s r o t p are o dn a yt l ngis c�in a e dv o r p mi r o re r g .dnil a h kCe hc t u o T s ’ su e o e h e r t lecnel xe w nr od-nu f o t eh ni a m s me t i ro f na t hped-ni re v o v ei w f o t eh ni am t ngis c�in a .st ni p o e� ni a m p e p a l a f o t eh e n w n ois re v dn a( t eh gt nih t ta h w i l l b e f o tso m tse re t ni ot t eh egare v a WPdro sse r s u )re nca b e emu ds ni en o d r o w – p s e .e d s ’ L te ol ko t a t i ni a t i b e r o m t .h ped WORDPRESS IS ABOUT TO GET WINGS e drpa C m o ot st i ,s er sed sec or p PPH 7 si yglniza b .t s af e� n om oc su s nen oc s se me ot b e Compared ttah s ’ t i t a tel s a t w eci sa t s af so rca t eh ,d b r o a dn a se riu e q r t i a yl ln t sb a u s re wf sec ru se o r ot u aca ylt l ete c xe u oc ed – otw sr caf o t t ta h e r a t lecnel xe e n sw r o f WPdro se r s su re sa w n oh s ni to its t eh ylre a tse t st u lse r w b ol e : predecessors, PHP 7 is blazingly fast. 9 WP ENGINE | TORQUE KOBE s repole veD se rPd ro W ro F PHP detneirO- tcejbO o T ediuG etamit lU T stse m of r Z d ne dn a WP Egenin gn o m a( y n a m s t re h o ) ev a h r�n e m doc t eh t ngis c�in a st ne me v o r p mi ew nca pxe e c t ni tb ho p s e e d dn a ,ec n r a mp o re f dn a t eh WPdro sse r e Cr o me at sah b e ne gre ni v b e a y aw a t a gtni te g t eh r mot f l a p ye da r r o f t eh e n w n ois re v ecnis 5102-dim . WordPress performance is signifcantly improved. THERE ARE TWO KEY POINTS TO EMPHASIZE HERE: .1 e re � e r a d r aw k b ca el tb i p a m oc ni se g n a h c t tah gu ni l p dn a etme h p sreol ed v w i l l ev a h ot t ea k ot ni t nu oca gni o g r . d o r f aw .2 WPdro ’ se r w n o t tne m im oc ot d r aw k b ca t yi il t b i p a m oc si sa gn o t rs sa re v – tb ho PPH 7 dn a sue v oi r p sn ois re v w i l l eu t ni n oc ot b e r.pe d t o p u s From the average end user point of view, it’s fair to say PHP 7 will be a slow burn in terms of when they really see the advantages. taen lodc A e ec v nedi mof r dnu o r a t eh be w st se gu s g e tre h si t is l l a tol f o kr o w ot b e en od n o rul a p p o setme h dn a sgu ni l p e r ob fe y n a m e r a ye da r r o f t eh e n w .stse n o h WP Eg enin sah ic� a ylol l el o d r t u o r p top u s f o PPH 7 ot a l l st i .s re m o t c s u gnid r oc A ot t rie h TC O , nsa o J C ,ne h o W“ ev ’ e eda m t i t on yn l o e ysa ot tse t et is sese nida r r o f PPH 7 tu b yl eb id rc ni e ysa r o f en o y n a n o ru o r mot f l a p ot et gim ar ot PPH ” .7 10 WP ENGINE | TORQUE

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.