Beginners Guide to Object Orientation in PHP


Published on

A very brief "crash course" in Object Orientation with PHP.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Programming Methodology – multiple interacting objects Self Sufficient – data is contained and manipulated by object “encapsulated data” - all data is stored within objects, and therefore organised and easily retrievable “protection of data” - access to data is controlled by the object
  • Modular – used in multiple apps
  • Talk through defining a class.
  • Instances of same class – same functionality, different data News article – headline, text, possible images – but differ
  • Method identified by parentheses
  • Beginners Guide to Object Orientation in PHP

    1. 1. Beginners Guide to Object Orientation In PHP by Rick Ogden for PHPNW09
    2. 2. What is Object Orientation? <ul>Object-Oriented Programming is a programming methodology that consists of multiple interacting objects, each completely self-sufficient. <li>This allows for flexible, expandable programming
    3. 3. Encapsulated code
    4. 4. Protection of data
    5. 5. Many more things beyond the scope of this tutorial </li></ul>
    6. 6. Class <ul>A class is a blueprint of an object, and is the basis of what the object will consist of. It contains two major entities: <li>Properties
    7. 7. Methods
    8. 8. A class is self sufficient by nature, and therefore can be implemented into multiple applications without modification. </li></ul>
    9. 9. Example of a Class <ul>Here we're going to create a new class for containing someone's profile information on a social networking website. </ul>
    10. 10. Object <ul>An object is created by creating a new instance of a class. Objects of the same class have exactly the same functionality, but the properties within the object are what makes them different. Eg. A news article on a website may be an object from a NewsArticle class, but the contents of the article will differ from another news article </ul>
    11. 11. Referencing <ul>In order for an object to be useful, you need to be able to call its contents. For this, PHP uses the arrow operator ( -> ). <li>$object->property;
    12. 12. $object->method(); </li></ul>
    13. 13. Self Referencing <ul>Throughout the instance of an object, chances are it will need to reference itself (to get its properties, or call its own methods). In order for an object to reference itself, the variable $this is used in the class. <li>$this->property;
    14. 14. $this->method(); </li></ul>
    15. 15. Properties <ul><li>Properties are class-wide variables.
    16. 16. They are often initialised when an object of the class is created (although they do not have to be)
    17. 17. They are defined at the top of the class
    18. 18. Methods can alter and interact with these properties throughout the existence of the object </li></ul>
    19. 19. Adding Properties <ul>We will add some properties to our Profile class. Of course the properties are not limited to the ones here: </ul>
    20. 20. Methods <ul>A method is a piece of code within a class which performs a task or calculation. These are similar to functions. It can: <li>Interact and modify properties of the object
    21. 21. Take arguments on execution
    22. 22. Return a value after execution
    23. 23. None of these are compulsory (although if it doesn't do any of these, it's a bit useless!) </li></ul>
    24. 24. Method Uses <ul>Methods are used for a number of different things. These include: <li>Retrieve data from a property in a “read only” fashion
    25. 25. Format data
    26. 26. Alter properties in a controlled way </li></ul>
    27. 27. Method: Parameters <ul><li>A method can include parameters (exactly like functions)
    28. 28. Parameters can either be required, or have a default value </li></ul>
    29. 29. Constructor <ul>The constructor is called when the object is initialised. <li>A constructor often takes parameters to initialise some (if not all) of the properties of that object
    30. 30. It is identified in a class as it has the method name __construct (for backwards-compatibility, a method with the same name as the class also works) </li></ul>
    31. 31. Our class so far <ul><li>I've added a constructor to initialise the properties
    32. 32. Added a method to return the full name of the person whose profile it is. </li></ul>
    33. 34. Instantiate an Object <ul><li>To create an object from a class you use the “new” keyword. </li><ul><li>$object = new MyClass(); </li></ul><li>This creates a new object and calls the constructor
    34. 35. Any arguments that need to be given to the instructor are given on creation.
    35. 36. We will store our class in Profile.php </li></ul>
    36. 37. Why Use Encapsulation <ul>Encapsulation gives the ability to hide data from outside of the object. <li>Gives the programmer control over what is inputted into properties (validation etc..)
    37. 38. What form data is when it is returned from the class
    38. 39. Ability to alter code within the class, without having to worry about needing to change code in other parts of the application </li></ul>
    39. 40. Public/Private/Protected <ul>Properties and methods can take one of 3 forms to encapsulate <li>Public: Property/method can be accessed from anywhere, inside or outside the object
    40. 41. Protected: Can only be accessed from within the class, or inherited class
    41. 42. Private: Can only be accessed from directly within the class (and not subclasses) </li></ul>
    42. 44. Inheritance <ul>Inheritance allows a programmer to reuse a class and expand it for a different purpose. Reasons: <li>Add code to a class to make it more specialised
    43. 45. Override existing code
    44. 46. Why reinvent the wheel? </li></ul>
    45. 47. Cons of Object Orientation <ul><li>Object Orientation does not come without its drawbacks
    46. 48. Main reason is it is less efficient than procedural code </li></ul>
    47. 49. Thank You Any Questions? For these slides and other things please visit my brand new website: