(An Extended) Beginners Guide to Object Orientation In PHP by Rick Ogden http://www.rickogden.com/
Drinking Game <ul><li>Take a drink when: </li><ul><li>I mention class, object, method, property
Every time a piece of example code is displayed </li></ul><li>Finish your drink when: </li><ul><li>I accidentally say “fun...
What is Object Orientation? <ul>Object-Oriented Programming is a programming methodology that consists of multiple interac...
Encapsulated code
Protection of data
Many more things beyond the scope of this tutorial </li></ul>
Mindset <ul>With OO you need quite a different mindset to programming using procedural methodology. <li>You need to think ...
What each entity does
What each entity needs </li></ul><li>From there you can go on to realise your ideas in “Classes”, which will in turn be in...
Examples of Objects
Examples of Objects
Examples of Objects
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 ...
Methods
A class is self sufficient by nature, and therefore can be implemented into multiple applications without modification. </...
Example of a Class <ul>Here we're going to create a new class for containing someone's profile information on a social net...
Object <ul>An object is created by creating a new instance of a class. Objects of the same class have exactly the same fun...
Referencing <ul>In order for an object to be useful, you need to be able to call its contents. For this, PHP uses the arro...
$object->method(); </li></ul>
Self Referencing <ul>Throughout the instance of an object, chances are it will need to reference itself (to get its proper...
$this->method(); </li></ul>
Properties <ul><li>Properties are class-wide variables.
They are often initialised when an object of the class is created (although they do not have to be)
They are defined at the top of the class
Methods can alter and interact with these properties throughout the existence of the object </li></ul>
Adding Properties <ul>We will add some properties to our Profile class. Of course the properties are not limited to the on...
Methods <ul>A method is a piece of code within a class which performs a task or calculation. These are similar to function...
Take arguments on execution
Return a value after execution (get)
None of these are compulsory (although if it doesn't do any of these, it's a bit useless!) </li></ul>
Method Uses <ul>Methods are used for a number of different things. These include: <li>Retrieve data from a property in a “...
Format data
Alter properties in a controlled way </li></ul>
Method: Parameters <ul><li>A method can include parameters (exactly like functions)
Parameters can either be required, or have a default value </li></ul>
Constructor <ul>The constructor is called when the object is initialised. <li>A constructor often takes parameters to init...
It is identified in a class as it has the method name __construct (for backwards-compatibility, a method with the same nam...
Our class so far <ul><li>I've added a constructor to initialise the properties
Added a method to return the full name of the person whose profile it is. </li></ul>
Profile.php
Instantiate an Object <ul><li>To create an object from a class you use the “new” keyword. </li><ul><li>$object = new MyCla...
Any arguments that need to be given to the constructor are given on creation.
We will store our class in Profile.php </li></ul>
controller.php
Execute Program
Execute Program
Encapsulation
Why Use Encapsulation <ul>Encapsulation gives the ability to hide data from outside of the object. <li>Gives the programme...
What form data is when it is returned from the class
Ability to alter code within the class, without having to worry about needing to change code in other parts of the applica...
Public/Private/Protected <ul>Properties and methods can take one of 3 forms to encapsulate <li>Public: Property/method can...
Upcoming SlideShare
Loading in...5
×

(An Extended) Beginners Guide to Object Orientation in PHP

2,498

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.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,498
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
67
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

(An Extended) Beginners Guide to Object Orientation in PHP

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

    Clipping is a handy way to collect important slides you want to go back to later.

×