|

Hunt Down The Nouns

hunt down the nouns

When starting with programming in an object oriented style, it is often said to hunt down the nouns. What does that really mean though? Well, let’s try out a few things in code to see what we’re talking about. This technique is great for when you are at the beginning level of working with object oriented php and will help to get you thinking in a way that will help to identify good class names for your projects.

Hunt Down The Nouns To Help Name Your Class

Recall, the class is the starting point for object oriented programming. So let’s think about something to build, and think of a noun to start with. Let’s consider we have some chores to do. Well, there is a noun, Chore. Let’s make a Chore class that will act as the blueprint for what a chore might look like.

A Class Can Have Properties

This little snippet just tells us that our Chore is going to have a description. So what is the deal with that word public? That deals with something we call encapsulation, and we’ll review that when we’re ready. For now, all we need to know is that our class has a description property. Since we have a description for a chore, let’s go ahead and hard code a value into that description. What’s a good description for a chore? How about, ‘Clean the dishes’.

Now if we have a class like this with a description and a value within it, how the heck do we make use of that information? How can we access it? The answer is to create a new instance of the class. A new instance of the class is referred to as an object. Once we have an object, we can make use of any properties inside it. This is how we might do that.

The $chore variable now actually holds an instance of, or object, of the class Chore. Great! Since we have that instance, we can now access the properties within it.

Now check it out. Our class is akin to a blueprint, but there is a value hard coded into our description. Does that make sense? Does a blueprint for a house contain the color of it’s siding? Probably not. One house might have blue siding, while another might have red. It makes sense then that our description should have a way to be able to decide what value it will have. In addition, think about what would happen if we instantiate, or New Up a bunch of objects or instances of our Chore class. Are we going to have 10 chores where all of them are to ‘Clean the dishes’? Of course not. We need a way to set the description for all of the various objects that we are going to create. A great way to assign a value to a property as soon as the object is created is by use of a constructor. Let’s see what that looks like.

When we new up an object of the Chore class at this point, the program will immediately echo out ‘Just Like Magic!’. How does this happen? The constructor is a special type of method that was designed to trigger immediately upon creation of a new instance of a class. Note that you must begin the constructor method with two underscores (which makes it look like one larger underscore) and then the word construct. What we have now is not all that helpful. What we want is a way to assign a value to our description when we create a new object. We want to be able to do things like the following.

This is a nice way to create three Chores and they each have a unique description. As it stands now, this code will not work. We need to update our class code, so that when we new up an instance of that class, it will be able to accept a value and assign it to the description property. This is how we can do that very thing by making use of the constructor.

The Magical Constructor Function

Notice that the constructor now has an argument. That is the $description that you see between the ( and the ). This represents the text that we provide when we new up an object, or a new instance of the Chore class. This text then gets assigned to the description property of that instance by way of $this->description. At this point, we are making use of $this, which has brought many a headache, confusion, and gnashing of teeth to the beginning object oriented programmer. When we use $this, we are making use of a way to distinguish between multiple instances of the same class. Just up above we created three instances of the Chore class right in a row. It is by the use of $this, that the assignment of the description value is handled correctly to each of the three objects. We can prove this by accessing the value of each description property on each object like so:

Here we can see that we currently have three Chores, and they all have a unique description property.

Altering Object State With Methods

There should also be a way to keep track of which chores are completed or not. Let’s see how we might do that.

The resulting sentence reads, “Right now, I need to Paint the shed. It is true that I have finished the first chore.” Since we made a call to the complete() method, the description property was changed from false to true. So now, we have a way to maintain the status of a chore and where it is completed or not.

Hunt Down The Nouns Summary

In this tutorial we got back to basics with object oriented php. We had a look at the concept of hunting down the nouns to help with naming conventions for classes in our code. We saw that once we have a class, we can create a new instance of that class called an object. In fact, we also saw that we may have multiple objects or instances of the same class, and that the keyword $this allows us to differentiate between the different objects currently active. Finally, the class we made use of was built with a basic property and method to deal with the state and description of a Chore.