Your SlideShare is downloading. ×
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

Beginners Guide to Object Orientation in PHP


Published on

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

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

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
  • Transcript

    • 1. Beginners Guide to Object Orientation In PHP by Rick Ogden for PHPNW09
    • 2. 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
      • 3. Encapsulated code
      • 4. Protection of data
      • 5. Many more things beyond the scope of this tutorial
    • 6. 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
      • 7. Methods
      • 8. A class is self sufficient by nature, and therefore can be implemented into multiple applications without modification.
    • 9. Example of a Class
        Here we're going to create a new class for containing someone's profile information on a social networking website.
    • 10. 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
    • 11. 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;
      • 12. $object->method();
    • 13. 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;
      • 14. $this->method();
    • 15. Properties
      • Properties are class-wide variables.
      • 16. They are often initialised when an object of the class is created (although they do not have to be)
      • 17. They are defined at the top of the class
      • 18. Methods can alter and interact with these properties throughout the existence of the object
    • 19. Adding Properties
        We will add some properties to our Profile class. Of course the properties are not limited to the ones here:
    • 20. 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
      • 21. Take arguments on execution
      • 22. Return a value after execution
      • 23. None of these are compulsory (although if it doesn't do any of these, it's a bit useless!)
    • 24. Method Uses
        Methods are used for a number of different things. These include:
      • Retrieve data from a property in a “read only” fashion
      • 25. Format data
      • 26. Alter properties in a controlled way
    • 27. Method: Parameters
      • A method can include parameters (exactly like functions)
      • 28. Parameters can either be required, or have a default value
    • 29. 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
      • 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)
    • 31. Our class so far
      • I've added a constructor to initialise the properties
      • 32. Added a method to return the full name of the person whose profile it is.
    • 33.  
    • 34. 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
      • 35. Any arguments that need to be given to the instructor are given on creation.
      • 36. We will store our class in Profile.php
    • 37. 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..)
      • 38. What form data is when it is returned from the class
      • 39. Ability to alter code within the class, without having to worry about needing to change code in other parts of the application
    • 40. 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
      • 41. Protected: Can only be accessed from within the class, or inherited class
      • 42. Private: Can only be accessed from directly within the class (and not subclasses)
    • 43.  
    • 44. 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
      • 45. Override existing code
      • 46. Why reinvent the wheel?
    • 47. Cons of Object Orientation
      • Object Orientation does not come without its drawbacks
      • 48. Main reason is it is less efficient than procedural code
    • 49. Thank You Any Questions? For these slides and other things please visit my brand new website: