I started writing computer programs solely for pleasure.
Initially, I wanted to play little tricks on my friends and do other pranks, it occurred to me a little late that i was going to take computer programming seriously in the immediate future.
Sometime this year, someone offered me a one time job, my first web development job actually –– he asked me to build a basic website and I thought it would be a great time to put everything I had learnt to exercise.
It took me roughly two weeks to complete the project. In the end, I had produced something good enough to be used and had also boastfully defied all the rules governing OOP and programming in general. Well, I guess I can’t be entirely blamed, 95 percent of the code was comprised of random stackoverflow answers.
My belief at that time was: if it looks like it works then it probably does.
Much later, I had the opportunity to work with an actual software driven company and it was then it dawned on me that doing things the right way mattered in computer programming; nobody wants to get involved with spaghetti code.
Realizing this, I began working intentionally to make my code more readable, adopted the habit of indenting and also made sure to comment every vague block of code in my programs but still, my efforts weren’t enough.
Some days, I looked at the code of the better developers around me and was met by some keywords I had used too often but never really knew what they meant. I wasn’t really familiar with the OOP fundamentals either and that was the most disturbing of my problems. I tried studying and understanding OOP at that time and it proved difficult like every other thing.
This article will introduce you to the basic concepts of OOP with examples but in a way I would have liked to read it when I was having OOP struggles.
Let’s begin by understanding what object oriented programming is about.
Object oriented programming is a programming style that supports the concept of defining all the properties and functions of an object within a class, this ensures that code is reusable and is a far more efficient approach to programming than previous paradigms.
Now this leads to the question, what is a class and what should a class contain? How do we know we have enough properties and functions to be grouped together as a class?
To answer these questions, let’s think this way; a class can contain all the code that deals with a certain element of a program. An example to explain this would be: Assume we have a general program that does lots of things but we have a smaller part of that program that defines a 'user' element, we can group all our code relating to the user properties and user functions within a single class and this would be beneficial to our program as a whole in lots of ways.
Code examples are written in PHP and kept short.
We should name a class according to what it is, for readability purposes and also, it is customary to begin the class name with a capital letter, if a class name contains two words or more then it should be written in upper camel case.
class User {
// The code
}
A class can have properties; properties are variables within a class, these variables may be initialized to contain values or may be left uninitialized (In some programming languages, they have to be initialized before the source code can compile.)
We can also specify how accessible these properties are by using access modifier keywords.
class User {
protected $age;
public $name;
public $clothed = true;
}
Now here’s the fun part, we can go ahead to create objects of the same class and each object would have its own set of properties unique to just that object.
$firstUser = new User ();
$secondUser = new User ();
$thirdUser = new User ();
Now you may be wondering, what good are classes and why do we need to create instances (objects) of classes anyway? Think about it this way; In the past, the procedural approach to programming allowed all variables and functions to live together on the global scope of the entire program, that is, they all lived on one surface and could possibly interact with one another just by calling their names and such.
With classes, this isn’t so; a class defines a scope outside the reach of the global scope, every operation carried out within the class scope is completely hidden from the global scope. The code nested within the class scope is encapsulated by the class and unavailable for use to the global scope, in order for the code within the class to be accessed and used, a class instance must be created. This class instance is called an object.
We may create as many objects of a class as we wish and each object differs from the other. On creation or instantiation of an object, the object inherits all properties from its class and can receive unique values for its own properties.
It is worth remembering that a class defines the structure and behavior of the objects that would be created from it.
Getting the value within an object’s property is easy. In PHP it can be accomplished by referencing the object’s name, attaching a dash greater than sign(->), then the property name.
echo $firstUser->age;
echo $secondUSer->name;
echo $thirdUser->clothed;
Result:
12
Jordan
false
It is also worth remembering that the $ sign is only attached to the object’s name but not the name of the property.
Setting a new value to an object’s property isn’t entirely different, we just attach the assignment operator to the right and the value gets assigned to the object’s property.
$firstUser->age = 20;
$secondUSer->name = “Castiel”;
$thirdUser->clothed = true;
Now printing the property of these objects would produce:
Result:
20
Castiel
true
That was fun to do!
Classes are wonderful to use, the idea behind properties is also scintillating but think about this. Imagine a class that defines users, this class says that all users must have a name and an age property, that’s great but this class doesn’t define the functionalities associated with a user object, objects are most likely to carry out operations using functions and these functions are called methods, the class should define methods for its objects.
Examples of functions for the user class performed by methods would be 'create a post', 'make a comment' and other similar ones.
class User{
protected $age;
public $name;
public $clothed = true;
public function makePost() {
// code for making a post go here;
}
We just included a simple makePost() method to our user class, notice that we prefix the method with the public access modifier to indicate that this method can be called from the global scope by a concrete instance of a class.
It is customary to name a method in lower camel case. Methods should be named according to the functions they perform and kept brief.
We call methods in a similar way as we refer to object properties; the object’s name, dash greater than sign(->) and the method’s name.
$firstUser = new User();
$firstUser->makePost();
The first line of code above created an object of the user class, while the other line invoked the makePost() method. In actual programming, the makepost() method will better have a parameter to receive the text made by a user and maybe save it to a database but these are just examples and everything is kept very simple since this is just an introduction to the basics of OOP.
Now let’s look at how much we have done, we have learnt about the basic idea of OOP and now know what OOP means in clearer terms, we have also seen why classes and objects are great for modern programming. Well-done, we’ve done so much! This should help lay the foundation if you wish to know more about OOP.
Top comments (3)
Most definitely, Chioke!
That was awesome Jordan! We hope to see more of this from you again. May be a part two.
That was good. In part 2 cover inheritance and traits. In part 3 cover composition vs inheritance.