Embed Size (px)
Transcript of Fowa Expo08
- 1. PHP5 For an easy coding life Keir Whitaker - Carsonified Future of Web Apps Expo London 2008
- 2. Welcome to FOWA Thanks for coming
- 3. Who am I? My name is Keir and I work at Carsonified Started with HTML 4 in 1999 Progressed to ASP in 2000 Managed web projects for large East London NHS trust Freelance developer from 2005 - 2008 using ASP.Net, MS SQL, MySQL and PHP Joined Carsonified in 2008 Help with our web projects and events Co-developed a Twitter mash up called MATT using Django
- 4. And you are? Say hello :)
- 5. What well look at today Development environments From PHP4 to PHP5 Classes and Object Orientated Programming MVC = Model View Controller CRUD & Models SMARTY Requests and Responses Third party code - some examples Simple XML and APIs
- 6. Development Environments Choosing the right tools
- 7. Useful Tools Code Editor (Its personal) Mac - TextMate, DreamWeaver, SKEdit PC - DreamWeaver, UltraEdit, NotePad MySQL PHP MyAdmin (web based) Mac - CocoaMySQL FTP Client Mac - Transmit, SecureFTP (FREE) PC - Cute FTP, FileZilla (FREE) Local Server Environment Mac - MAMP/MAMP Pro PC - WAMP Server Source Control Subversion, Git and a host of toerhs Useful Add Ons Web developer toolbar Firefox Firebug for AJAX development
- 8. From PHP4 to PHP5 Heres how I use to develop in PHP4 Functions, functions and more functions Little thought to how functions could relate to each other Lots of included files in php files in lots of different directories The occasional use of a class (usually from the web) Little or no separation of code, content and presentation Small changes = lots of work as I needed to edit multiple files
- 9. From PHP4 to PHP5 Heres how I develop now using PHP5 One PHP file above the server root Clear seperation between content and presentation Heavy use of Object Orientated Programming SMARTY Template Engine (OK its PHP4!) Classes to handle interaction with MySQL Simplified inclusion of third party code Easier to manage, easier to code, easier life!
- 10. Code Demo #1 Procedural programming with PHP4
- 11. Classes and OOP Whats all the fuss about?
- 12. What is OOP? Object-oriented programming (OOP) is a programming paradigm that uses quot;objectsquot; and their interactions to design applications and computer programs. It is based on several techniques, including encapsulation, modularity, polymorphism, and inheritance. It was not commonly used in mainstream software application development until the early 1990s. Many modern programming languages now support OOP. - http://en.wikipedia.org/wiki/Object_oriented
- 13. So what does that mean? OOP allows us to create reusable chunks of code and keep our code DRY (Dont Repeat Yourself) Modularise our applications functionality Allow to code to interact in a safe way Keep code and presentation seperate Easily extend our applications
- 14. Classes & Objects A class is a blueprint for an object. A class merely describes a type of object Before you can use a class you need to Create an object based on the class. Once created this is called a class instance To do this you instantiate the class Once instantiated you can call the methods defined in that class (methods are just functions)
- 15. Whats in a class? Classes USUALLY contain Methods These are PHP functions. No real difference to writing a standard PHP function. They can take arguments and return values. Class methods have access to a special variable called $this Variables for use within the class NB: Its important to understand that variables can vary from instance to instance. This is what makes classes so valuable.
- 16. Whats in a class? cont... Classes MAY contain A Constructor This is used to set up the class in some way at the point of instantiation They do not have a return value A Destructor This is automagically called when the class is destroyed. Its available but not often used in practice.
- 17. Code Demo #2 A basic PHP5 class
- 18. Always return values Avoid output in classes e.g. dont use echo and printf It reduces flexibility Return values via the class This allows further transformations to be undertaken or the value could be used in a different way entirely. For example in an e-mail
- 19. Understanding scope Variable scope Global Function Class Its easy to get caught out With OOP you tend to only use the Class scope
- 20. What is $this-> all about? $this is a special variable Used all the time in PHP5 classes It refers to a particular instance of a class Used to call methods and get/set variables in that instance
- 21. Code Demo #3 Understanding $this
- 22. Visibility in classes The visibility of a property or method can be defined by prefixing the declaration with the keywords: public, protected or private Public declared items can be accessed everywhere Protected limits access to inherited and parent classes (and to the class that defines the item) Private limits visibility only to the class that defines the item This affects how you interact with a class instance i.e. how you get and set variables and call methods
- 23. Code Demo #4 Understanding visibility
- 24. Inheritance Classes can inherit functionality and variables from other classes Allows us to extend the functionality of one class without changing it e.g. extending a data access class to add paging Giving all your classes access to a number of base functions used on every section of your site Extend a base controller - more later
- 25. Code Demo #5 Class inheritance
- 26. PHP5 Autoloading Fact: Your code needs to know where your classes are in the file system This usually requires using the require_once(path_to_file) function to include the file If we stick to conventions we can avoid having to remember to include our class files every time
- 27. Code Demo #6 Auto loading