php - procedural to object-oriented
DESCRIPTION
Learn the power of object-oriented programming in PHP5 and the many benefits it offers over the more traditional PHP procedural programming style. This session will include a light introduction to object-oriented concepts and will provide real-world concrete examples of the benefits it can offer you and the PHP projects you work on.TRANSCRIPT
![Page 1: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/1.jpg)
![Page 2: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/2.jpg)
The Current Situation Object-Oriented Introduction
› Basics, concepts, terminology & examples Object-Oriented Benefits
› Code re-use, portability, structure, meaning, control, separation of responsibility, time, reduce dependencies and more
![Page 3: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/3.jpg)
Most scripts & tutorials all procedural or show bad understanding of OO
PHP’s unique history› Template language & PHP/FI 2.0› “evolved” over time with OO features
Good OO finally starting to emerge› PHP5 released in 2004› Zend Framework› Symfony Project
![Page 4: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/4.jpg)
![Page 5: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/5.jpg)
That was easy!Add “Object-Oriented PHP” to your resume!
![Page 6: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/6.jpg)
It’s much more than just syntax – It’s a mindset
Object-Oriented programming gives meaning to your data› Represents real-world objects when
possible
Where have we seen this before?
![Page 7: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/7.jpg)
Semantic markup Think:
› <blockquote> for quotes, not indentation› <address> for addresses, not <p> or
<div>› <cite> instead of <i> for citations
Goal is to for code to convey meaning
![Page 8: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/8.jpg)
• Create meaning and structure• Separate responsibilities• Reduce/Isolate dependencies• Create common public interfaces• Clear, self-documenting code
![Page 9: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/9.jpg)
Code objects cannot always represent real-world objects› Application flow› Structure› Delegation› Separation of responsibilities
How do we solve these problems?› Design Patterns
![Page 10: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/10.jpg)
Model-View-Controller (MVC) Active Record Data Mapper Adapter Registry Factory Observer
![Page 11: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/11.jpg)
What OO programming is not – where many PHP projects go wrong
![Page 12: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/12.jpg)
![Page 13: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/13.jpg)
Decorator
![Page 14: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/14.jpg)
What is User?› A finder/utility class?› No clear definition or
purpose
“bob” is a User 1 instance = 1
user Properties of a
user Functions of a user
![Page 15: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/15.jpg)
The power of applying object-oriented design to your application
![Page 16: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/16.jpg)
Objects add automatic clarity Stick to standards and naming
conventions› camelCase› Zend Coding Standards
Function naming as verbose as possible› getPostBySlug($slug)› findPostsWithTag($tagName)
![Page 17: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/17.jpg)
![Page 18: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/18.jpg)
![Page 19: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/19.jpg)
![Page 20: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/20.jpg)
Parent > Child relationship between objects› Child extends Parent
Child inherits all parent methods› Child can override parent methods when
required Changes can be reflected across your
entire application with relative ease
![Page 21: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/21.jpg)
![Page 22: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/22.jpg)
Hides actual implementation Reduces or isolates dependencies to
lower levels Reduces or eliminates extensive future
changes in code base
![Page 23: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/23.jpg)
› Adding extra headers is a mess (CC, BCC)› No built-in file attachment options› Limited configuration options
(Sendmail/SMTP)› Very inefficient for large amounts of email
![Page 24: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/24.jpg)
› Custom Email() object› Clear, descriptive syntax› Unlimited configuration options
mail(), SMTP, database, external server, etc.
› File attachments can be easy with function
![Page 25: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/25.jpg)
Did you notice the dependencies?
![Page 26: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/26.jpg)
Remove the database dependencies by using a standard interface› PDO provides one (bundled with PHP5)
Use dependency injection to expose dependencies
![Page 27: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/27.jpg)
Pass connection into constructor and store it as a class property
Replace mysql_* functions with standard functions from PDO using connection
![Page 28: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/28.jpg)
Database adapters are interchangeable
![Page 29: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/29.jpg)
Interchangeable code components› Standard interface› Single dependency
Separate “engine” from implementation
![Page 30: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/30.jpg)
Different payment processors are a simple drop-in replacement
![Page 31: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/31.jpg)
Objects should serve a single purpose› Separate code into manageable chunks
MVC – Good example› Model – Database› View – Display› Controller – Request/Flow
Resist temptation of globals everywhere› “global $var”, $_SESSION, $_REQUEST, etc
![Page 32: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/32.jpg)
![Page 33: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/33.jpg)
![Page 34: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/34.jpg)
Building a Data Mapper with PHP5 and the Standard PHP Library› Friday Oct. 10 @ 10:30am
![Page 35: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/35.jpg)
www.php.net/php5› Full official PHP5 feature documentation
Communities› Sitepoint – sitepoint.com› PHPit – phpit.com› PHP|Architect – phparch.com› Zend Developer Zone - devzone.zend.com
![Page 36: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/36.jpg)
Vance Lucas, 23› Blog: www.vancelucas.com› Email: [email protected]
Making websites since age 12› Started learning PHP when PHP3 was new
Oklahoma Baptist University – Shawee› Bachelor of Business Administration
Currently work at Back40 Design, Inc.› www.back40design.com
![Page 37: PHP - Procedural To Object-Oriented](https://reader038.vdocument.in/reader038/viewer/2022102900/54c41f914a795968638b456c/html5/thumbnails/37.jpg)