Membership
Main Menu
Forum Boards
Stats
- 18 tutorials
- 72,339 members
- 696,896 forum posts
- 11 blog posts
Tutorials
OO PHP Part 1: OOP in Full Effect
Views: 38562
1. The Very Basics
1.1 A tiny bit of theory
So what is an object anyway? In a definition proposed by IBM’s Grady Booch in 1991:
An object is an instance of a class with the following essential properties:
- Each object has its own identity.
- Each object can have certain behaviour.
- Each object is in a state.
Let’s do a quick elaboration on these properties.
Each object has its own identity.
'Having its own identity' implies that even if two objects are structurally the same (of the same class and in the same state), they are still not identical.
Each object can have behaviour.
This behaviour is used, thus this is also referred to as “offer services”. Basically an object has functions and variables local to that object, referred to as methods and properties (more on that in the next section), which define and influence this behaviour.
Each object is in a state.
The before mentioned properties (local variables) define the state an object is in. This state can influence the behaviour (the methods act differently depending on the properties).
In addition, objects often represent some concept, an object in the more literal sense.
I know you all can’t stand all this abstract talk and are itching for a code example, so here we go…
1.2 Hold on, read this first
Wait. Before I show you ANY code AT ALL, note that in this tutorial I violate a truckload of ‘good practice’ heuristics. This is done to be able to explain you the basics. Take note of the fact that this tutorial is intended to show the features available to you in PHP, it does not promote any actual practice. Don’t worry, I will definitely get to that later.
1.3 Absolute basic syntax
Instantiating an (creating a new, unique) object:
Executing a function in the object (a method):
Assigning the return value from a method to a variable:
Setting and retrieving the current value of a property (a variable local to the object, not unlike an array index):
Hopefully you now have a basic understanding of what objects are and how to operate on them. Next we’re looking at defining the state and behaviour of objects.
