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

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