Your SlideShare is downloading. ×
(An Extended) Beginners Guide to Object Orientation in PHP
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

(An Extended) Beginners Guide to Object Orientation in PHP


Published on

The talk I gave at the December meeting for PHPNW. This is a slightly extended version of the talk I gave at the conference.

The talk I gave at the December meeting for PHPNW. This is a slightly extended version of the talk I gave at the conference.

Published in: Technology

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. (An Extended) Beginners Guide to Object Orientation In PHP by Rick Ogden
  • 2. Drinking Game
    • Take a drink when:
      • I mention class, object, method, property
      • 3. Every time a piece of example code is displayed
    • Finish your drink when:
      • I accidentally say “function” instead of “method”
  • 4. What is Object Orientation?
      Object-Oriented Programming is a programming methodology that consists of multiple interacting objects, each completely self-sufficient.
    • This allows for flexible, expandable programming
    • 5. Encapsulated code
    • 6. Protection of data
    • 7. Many more things beyond the scope of this tutorial
  • 8. Mindset
      With OO you need quite a different mindset to programming using procedural methodology.
    • You need to think about things as “entities”:
      • What each entity is
      • 9. What each entity does
      • 10. What each entity needs
    • From there you can go on to realise your ideas in “Classes”, which will in turn be instantiated into “Objects”
  • 11. Examples of Objects
  • 12. Examples of Objects
  • 13. Examples of Objects
  • 14. Class
      A class is a blueprint of an object, and is the basis of what the object will consist of. It contains two major entities:
    • Properties
    • 15. Methods
    • 16. A class is self sufficient by nature, and therefore can be implemented into multiple applications without modification.
  • 17. Example of a Class
      Here we're going to create a new class for containing someone's profile information on a social networking website.
  • 18. Object
      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
  • 19. Referencing
      In order for an object to be useful, you need to be able to call its contents. For this, PHP uses the arrow operator ( -> ).
    • $object->property;
    • 20. $object->method();
  • 21. Self Referencing
      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.
    • $this->property;
    • 22. $this->method();
  • 23. Properties
    • Properties are class-wide variables.
    • 24. They are often initialised when an object of the class is created (although they do not have to be)
    • 25. They are defined at the top of the class
    • 26. Methods can alter and interact with these properties throughout the existence of the object
  • 27. Adding Properties
      We will add some properties to our Profile class. Of course the properties are not limited to the ones here:
  • 28. Methods
      A method is a piece of code within a class which performs a task or calculation. These are similar to functions. It can:
    • Interact and modify properties of the object (set)
    • 29. Take arguments on execution
    • 30. Return a value after execution (get)
    • 31. None of these are compulsory (although if it doesn't do any of these, it's a bit useless!)
  • 32. Method Uses
      Methods are used for a number of different things. These include:
    • Retrieve data from a property in a “read only” fashion
    • 33. Format data
    • 34. Alter properties in a controlled way
  • 35. Method: Parameters
    • A method can include parameters (exactly like functions)
    • 36. Parameters can either be required, or have a default value
  • 37. Constructor
      The constructor is called when the object is initialised.
    • A constructor often takes parameters to initialise some (if not all) of the properties of that object
    • 38. 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)
  • 39. Our class so far
    • I've added a constructor to initialise the properties
    • 40. Added a method to return the full name of the person whose profile it is.
  • 41. Profile.php
  • 42. Instantiate an Object
    • To create an object from a class you use the “new” keyword.
      • $object = new MyClass();
    • This creates a new object and calls the constructor
    • 43. Any arguments that need to be given to the constructor are given on creation.
    • 44. We will store our class in Profile.php
  • 45. controller.php
  • 46. Execute Program
  • 47. Execute Program
  • 48. Encapsulation
  • 49. Why Use Encapsulation
      Encapsulation gives the ability to hide data from outside of the object.
    • Gives the programmer control over what is inputted into properties (validation etc..)
    • 50. What form data is when it is returned from the class
    • 51. Ability to alter code within the class, without having to worry about needing to change code in other parts of the application
  • 52. Public/Private/Protected
      Properties and methods can take one of 3 forms to encapsulate
    • Public: Property/method can be accessed from anywhere, inside or outside the object
    • 53. Protected: Can only be accessed from within the class, or inherited class
    • 54. Private: Can only be accessed from directly within the class (and not subclasses)
  • 55.  
  • 56.  
  • 57. controller
  • 58. Execute the Controller
  • 59. Execute the Controller
  • 60. Attempt to access Private Property
  • 61. Attempt to access Private Property
  • 62. Inheritance
  • 63. Inheritance
      Inheritance allows a programmer to reuse a class and expand it for a different purpose. Reasons:
    • Add code to a class to make it more specialised
    • 64. Override existing code
    • 65. Why reinvent the wheel?
  • 66.  
  • 67.  
  • 68.  
  • 69.  
  • 70.  
  • 71. Execute
  • 72. Execute
  • 73. Fluent Interfaces
    • An accessor method by its nature must return a value – a mutator method does not.
    • 74. Fluent Interfaces are a convenience for programmers (who are inherently lazy) to perform multiple mutations in one statement.
    • 75. $myObject->changeSurname('Ogden')->changeForename('Rick');
  • 76. Fluent Interfaces
    • This is achieved by having methods return “this”
    • 77. Any method that returns “this” can have a method call appended to it.
    • 78. return $this;
  • 79. Instance Control
      What happens if you want to make sure if you only want one instance of a Class?
    • Eg Database connection
    • 80. Use a Singleton!
    • 81. This is achieved using a static method.
  • 82. Static Method
      Static methods (unlike standard methods) can be called at the “Class level”. This means it does not require an object to be created for it to be called. This can be used to control the creation and use of an object.
  • 83. Static Referencing
    • As static methods are not part of objects, you do not use an arrow. You use a double colon:
      • $object->method();
      • 84. Class::method();
    • Also, for self referencing within the class, the keyword “self” is used (sort of like $this).
  • 85. Singleton Example
  • 86. Get/Create Singleton
      As there can only ever be one instance of a singleton, creation of instance and retrieval of instance is done by the same (static) method. From there on, the object can be interacted with as normal
  • 87. Cons of Object Orientation
    • Object Orientation does not come without its drawbacks
    • 88. Main reason is it is less efficient than procedural code
    • 89. For smaller projects, often means writing more code
  • 90. Thank You Any (other) questions? For these slides, example code and other things please visit my website: