Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PHP - Procedural To Object-Oriented


Published on

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.

Published in: Technology
  • Good presentation
    Are you sure you want to  Yes  No
    Your message goes here
  • This is utter rubbish, I'm sorry but I code php in a procedural way and it is alot faster and more scalable than an OOP approach. Just as a quick example I recently converted (for a client) a smarty OOP approach to a procedural approach. The smarty was averaging about 6 seconds load time for a page and killing the CPU of the server - the OOP was also very long and cumbersome and not easy to identify when working on someone else's code... I took it to procedural and shaved 5 seconds from the load time, 98% off the CPU load and the site is now manageable with clearlrly documented functions.

    This slideshow is full of holes (security and memory) - whether or not these are intentional I don;t know but perhaps show some secure programming when demonstrating the so called 'power' or OOP in php.

    Don't get me wrong .. OOP has it's place in programming but it certainly for the enterprise projects I write it is not better than procedural programming.
    Are you sure you want to  Yes  No
    Your message goes here
  • Goof presentation.
    Are you sure you want to  Yes  No
    Your message goes here
  • thanks, very helpful
    Are you sure you want to  Yes  No
    Your message goes here

PHP - Procedural To Object-Oriented

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