Thanks to visit codestin.com
Credit goes to www.slideshare.net

Beginners Guide to Object Orientation In PHP by Rick Ogden for PHPNW09
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
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.
Example of a Class Here we're going to create a new class for containing someone's profile information on a social networking website.
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
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();
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();
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
Adding Properties We will add some properties to our Profile class. Of course the properties are not limited to the ones here:
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

Beginners Guide to Object Orientation in PHP

  • 1.
    Beginners Guide toObject Orientation In PHP by Rick Ogden for PHPNW09
  • 2.
    What is ObjectOrientation? Object-Oriented Programming is a programming methodology that consists of multiple interacting objects, each completely self-sufficient. This allows for flexible, expandable programming
  • 3.
  • 4.
  • 5.
    Many more thingsbeyond the scope of this tutorial
  • 6.
    Class A classis a blueprint of an object, and is the basis of what the object will consist of. It contains two major entities: Properties
  • 7.
  • 8.
    A class isself sufficient by nature, and therefore can be implemented into multiple applications without modification.
  • 9.
    Example of aClass Here we're going to create a new class for containing someone's profile information on a social networking website.
  • 10.
    Object An objectis 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 orderfor an object to be useful, you need to be able to call its contents. For this, PHP uses the arrow operator ( -> ). $object->property;
  • 12.
  • 13.
    Self Referencing Throughoutthe 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.
  • 15.
    Properties Properties areclass-wide variables.
  • 16.
    They are ofteninitialised when an object of the class is created (although they do not have to be)
  • 17.
    They are definedat the top of the class
  • 18.
    Methods can alterand interact with these properties throughout the existence of the object
  • 19.
    Adding Properties Wewill add some properties to our Profile class. Of course the properties are not limited to the ones here:
  • 20.
    Methods A methodis 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.
  • 22.
    Return a valueafter execution
  • 23.
    None of theseare compulsory (although if it doesn't do any of these, it's a bit useless!)
  • 24.
    Method Uses Methodsare used for a number of different things. These include: Retrieve data from a property in a “read only” fashion
  • 25.
  • 26.
    Alter properties ina controlled way
  • 27.
    Method: Parameters Amethod can include parameters (exactly like functions)
  • 28.
    Parameters can eitherbe required, or have a default value
  • 29.
    Constructor The constructoris 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 identifiedin 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 sofar I've added a constructor to initialise the properties
  • 32.
    Added a methodto return the full name of the person whose profile it is.
  • 33.
  • 34.
    Instantiate an ObjectTo 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 thatneed to be given to the instructor are given on creation.
  • 36.
    We will storeour class in Profile.php
  • 37.
    Why Use EncapsulationEncapsulation 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 datais when it is returned from the class
  • 39.
    Ability to altercode within the class, without having to worry about needing to change code in other parts of the application
  • 40.
    Public/Private/Protected Properties andmethods can take one of 3 forms to encapsulate Public: Property/method can be accessed from anywhere, inside or outside the object
  • 41.
    Protected: Can onlybe accessed from within the class, or inherited class
  • 42.
    Private: Can onlybe accessed from directly within the class (and not subclasses)
  • 43.
  • 44.
    Inheritance Inheritance allowsa programmer to reuse a class and expand it for a different purpose. Reasons: Add code to a class to make it more specialised
  • 45.
  • 46.
  • 47.
    Cons of ObjectOrientation Object Orientation does not come without its drawbacks
  • 48.
    Main reason isit is less efficient than procedural code
  • 49.
    Thank You AnyQuestions? For these slides and other things please visit my brand new website: http://www.rickogden.com/

Editor's Notes

  • #3 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
  • #4 Modular – used in multiple apps
  • #5 Talk through defining a class.
  • #6 Instances of same class – same functionality, different data News article – headline, text, possible images – but differ
  • #7 Method identified by parentheses