Beginners Guide to Object Orientation in PHP

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Notes on slide 1

    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

    1 Favorite

    Beginners Guide to Object Orientation in PHP - Presentation 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
      • Encapsulated code
      • Protection of data
      • Many more things beyond the scope of this tutorial
    3. 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
      • Methods
      • A class is self sufficient by nature, and therefore can be implemented into multiple applications without modification.
    4. Example of a Class
        Here we're going to create a new class for containing someone's profile information on a social networking website.
    5. 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
    6. 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;
      • $object->method();
    7. 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;
      • $this->method();
    8. Properties
      • 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
    9. Adding Properties
        We will add some properties to our Profile class. Of course the properties are not limited to the ones here:
    10. 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
      • Take arguments on execution
      • Return a value after execution
      • None of these are compulsory (although if it doesn't do any of these, it's a bit useless!)
    11. Method Uses
        Methods are used for a number of different things. These include:
      • Retrieve data from a property in a “read only” fashion
      • Format data
      • Alter properties in a controlled way
    12. Method: Parameters
      • A method can include parameters (exactly like functions)
      • Parameters can either be required, or have a default value
    13. 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
      • 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)
    14. Our class so far
      • I've added a constructor to initialise the properties
      • Added a method to return the full name of the person whose profile it is.
    15.  
    16. 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
      • Any arguments that need to be given to the instructor are given on creation.
      • We will store our class in Profile.php
    17. 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..)
      • 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 application
    18. 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
      • Protected: Can only be accessed from within the class, or inherited class
      • Private: Can only be accessed from directly within the class (and not subclasses)
    19.  
    20. 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
      • Override existing code
      • Why reinvent the wheel?
    21. Cons of Object Orientation
      • Object Orientation does not come without its drawbacks
      • Main reason is it is less efficient than procedural code
    22. Thank You Any Questions? For these slides and other things please visit my brand new website: http://www.rickogden.com/

    + Rick OgdenRick Ogden, 1 month ago

    custom

    498 views, 1 favs, 2 embeds more stats

    A very brief "crash course" in Object Orientation w more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 498
      • 456 on SlideShare
      • 42 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 29
    Most viewed embeds
    • 41 views on http://www.rickogden.com
    • 1 views on http://www.fachak.com

    more

    All embeds
    • 41 views on http://www.rickogden.com
    • 1 views on http://www.fachak.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories