Tutorials

OO PHP Part 1: OOP in Full Effect

by John Kleijn on Jun 6, 2008 3:21:29 PM

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.

Comments

Looks like a nice intro loaded with information. Some things I see lacking in OOP with PHP is the use of get and set methods. When learning OOP in java this was a key thing we learned I just think it makes good practice.

1. Jeff Combs on Jun 6, 2008 8:17:36 PM

I agree wholeheartedly, but as it says in section 1.2: this tutorial only shows the language features.

Good practice will be a subject soon enough.

2. John Kleijn on Jun 7, 2008 3:43:41 AM

I can't wait for the rest of the series, I've been struggling to learn good OOP Application design for some time because I don't have any peers where I work who know the subject, given this first part I think I will learn a lot and really appreciate the author's efforts. Thank you!

3. phpzone on Jun 7, 2008 8:51:58 AM

You're welcome (though I can never have enough of gratitude). ;)

Note that part 2 is already online, and part 3 underway.

4. John Kleijn on Jun 7, 2008 11:51:03 AM

lovin this. :D

5. aim25 on Jun 22, 2008 11:46:05 AM

Cool tutorial, really useful and easy to understand

6. Jack Bert-Oswald on Jul 15, 2008 11:04:27 PM

Thanks man, really helpful. Tutorial is great but i think that would be great to add some more explaining on interface if you have time cauz i really didn't understand what should i do with the code you give as an example. Thanks for effort to make this tutorial.

7. HardCoreMore on Jul 18, 2008 2:44:31 AM

From 2003 I am great follower of php tutorials by phpfreaks. It's very useful the stuff on advanced. These kind of tutorials make us strong in fundamentals.

Good work.
sharma chelluri

8. sharma chelluri on Aug 1, 2008 8:52:31 AM

Great tutorial, but honestly, we're not programming PHP animals. In future tutorials, could you cut the crap about the dog and provide code that does something meaningful?

9. sneamia on Sep 13, 2008 5:12:13 PM

As a beginner I like analogies that make it easy to follow.

10. arwvisions on Oct 4, 2008 11:16:59 PM
Login or register to post a comment.