(An Extended) Beginners Guide to Object Orientation In PHP
byRick Ogden
http://www.rickogden.com/
Drinking Game
Take a drink when:I mention class, object, method, property
Every time a piece of example code is displayed
Finish your drink when:I accidentally say function instead of method
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
Mindset
With OO you need quite a different mindset to programming using procedural methodology.
You need to think about things as entities:What each entity is
What each entity does
What each entity needs
From there you can go on to realise your ideas in Classes, which will in turn be instantiated into Objects
Examples of Objects
Examples of Objects
Examples of Objects
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 (set)
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!)
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
Method: Parameters
A method can include parameters (exactly like functions)
Parameters can either be required, or have a default value
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)
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.
Profile.php
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 constructor are given on creation.
We will store our class in Profile.php
controller.php
Execute Program
Execute Program
Encapsulation
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
Public/Private/Protected
Properties and methods can take one of 3 forms to encapsulatePublic: 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)
controller
Execute the Controller
Execute the Controller
Attempt to access Private Property
Attempt to access Private Property
Inheritance
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?
Execute
Execute
Fluent Interfaces
An accessor method by its nature must return a value a mutator method does not.
Fluent Interfaces are a convenience for programmers (who are inherently lazy) to perform multiple mutations in one statement.
$myObject->changeSurname('Ogden')->changeForename('Rick');
Fluent Interfaces
This is achieved by having methods return this
Any method that returns this can have a method call appended to it.
return $this;
Instance Control
What happens if you want to make sure if you only want one instance of a Class?
Eg Database connection
Use a Singleton!
This is achieved using a static method.
Static Method
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.
Static Referencing
As static methods are not part of objects, you do not use an arrow. You use a double colon:
$object->method();
Class::method();
Also, for self referencing within the class, the keyword self is used (sort of like $this).
Singleton Example
Get/Create Singleton
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
Cons of Object Orientation
Object Orientation does not come without its drawbacks
Main reason is it is less efficient than procedural code
For smaller projects, often means writing more code
Thank You
Any (other) questions?
For these slides, example code and other things please visit my
website:
http://www.rickogden.com/
Click to edit the title text format
Click to edit the outline text formatSecond Outline LevelThird Outline LevelFourth Outline LevelFifth Outline LevelSixth Outline LevelSeventh Outline LevelEighth Outline LevelNinth Outline Level