Objectives & prerequisites
- Basic PHP coding: working with variables, constants, functions, loops, and control structures.
Objects
Object Oriented Programming (OOP) is an approach to software development centered around the concept of "objects" that relate to each other.
Objects often - but not always - correspond to things in the real world:
- Software for accountants could have objects to represent accounts, vendors, invoices, credit notes, and orders.
- Inside an application for hotel reservations you could expect to find objects that represent guests, rooms, reservations, or invoices.
Objects typically represent state and behavior:
- State: data (variables / properties)
- Behavior: functionality (functions / methods)
This is true for objects in the real world as well.
Example
A smartphone is a physical object that contains data (your photos, messages, and contacts) and functionality (it offers ways to manage the data, call contacts, take and edit pictures, and much more).
An instruction manual is a physical object that contains data, but it doesn't offer many features except maybe an index of keywords at the back that you can use to find what you're looking for.
My wooden box with business cards people have given me over the years is a physical object that contains data, but offers no functionality. It's still an object even though it only contains data. That is why we said earlier that an object typically contains data and functionality.
Types of objects
OOP is based on four foundational principles we'll discuss later:
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
On top of these four principles there are different approaches (also referred to as design patterns) to working with objects. You may encounter terms like "data object", "value object", "data access object", "data transfer object", or "business object".
These all reflect different ways of working with objects - some of which we'll cover in these materials.
Important
Don't be intimidated by words like "encapsulation", "polymorphism" or "data transfer object". Right now the import things to remember are:
- Objects often represent real-world things.
- Objects often contain a mix of data and functionality.
Classes and instances
Let's continue using smartphones as an example:
A new technology startup company produces one model of smartphone. Each phone it produces is built the same way following the same specifications called "Phone".
You can call those specifications a definition, or a template, or a blueprint.
In OOP such a blueprint is called a class or class definition.
Each completed phone is an object, more specifically an object of the class Phone.
Another way of saying this is that each phone is an object of the type Phone, or that each phone's object type is Phone.
Yet another way of saying the same is that each individual phone is an instance of the Phone class.
These are all different ways of saying the same thing.
No objects without classes
In OOP, any given object is always an instance of a certain class.
There are no objects without classes, but there can be classes without any objects (or rather instances of that class): you could have created the Phone class without actually creating any instances (individual phones) of that class.
This may sound strange, but this happens all the time.
For instance, when you newly install a CMS like Drupal, there is no content yet, but there are classes that represent different kinds of content ready to be used. When you create a new article via the web interface, a new object of the type Node is created, and when you add a new user account, a new object of the type User is created.
Defining a class
Let's start with a basic class definition. Throughout these materials you'll keep working on this class and add new things as you learn new concepts and techniques.
The following is the most basic class you can define. It doesn't do anything, but it is complete enough to allow you to create objects (instances) of this class.
Instantiating an object
With the class (definition) in place, you can create an instance of that class, a process called instantiating an object.
All of the following statements are true:
$myphoneis a variable of the type Phone.$myphoneis an object of the type Phone.$myphoneis an instance of Phone.$myphoneis an instance of the Phone class.
Using objects as function parameters
As objects are a type of variable, you can also pass objects to functions or methods:
In this example the $myphone object is passed to the print_r() function, which lets you recursively print complex variables like objects and arrays.
Activity
Create a phone object, and try to print it with:
- the
print()function - the
print_r()function - the
var_dump()function
Compare the different results.
Conclusion
At this point the Phone class is minimal but functional, and you can use it to instantiate objects.
The next step is to learn about adding data (properties) and behavior (methods).