Object-Oriented PHP: Classes

Before diving into Laravel or any PHP framework, it is necessary that we understand first the concepts of Object-Oriented Programming (OOP). I presumed that you're possibly coming from a more procedural world, a type of world where you place a bunch of PHP codes right above a block of HTML. I think everyone go through that phase where we have tons of procedural codes everywhere, no real separation of concerns, we repeat our selves over and over, none of the codes is reusable elsewhere and it all ends up to a very difficult to maintain application. Most of those problems if not all, can be fix through OOP. So let's get started!
First, let's review a basic class. To create a class you have to use the keyword
class
. You can think of a class as a template of what your objects might look like. For example, let's say you are building an app where the users can create, edit, and view their profile, in that case, it would make sense that you would have a User
class. Also, it's recommended to use a snake case when naming your class and the PHP file name will be the same as your class name. So, in this case lets create a User.php
file:<?php
class User {
}
This is how basic class looks like.Now that our class is ready, we are now going to add some properties. Think of a properties as a features of an object, like in a real world object, let say a box, what are the features of a box? A box has a color, width, height, so on and so forth. In our example a user might have a name property. So here's what that might look like:
<?php
class User {
public $name = 'John Doe';
}
As you noticed, there is a new keyword public
that we used. Don't worry, we are going to discuss encapsulation in our follow up lessons, but for now, when you say public
, it means that outside of that User
class, anyone can access that name
property. How exactly are we going to do that?Before we can access that property, we have to instantiate the class first, by using the
new
keyword:
<?php
class User {
public $name = 'John Doe';
}
$user = new User();
var_dump($user->name);
Now that we already have a $user
object, we can now access the name
property using the arrow syntax, like so: $user->name
Let's try to run it in the terminal to see the output:
D:\Tutorials\Laragle\PHP\OOP>php User.php
The output should look like this:
string(8) "John Doe"
As I mentioned earlier, a class is like a template where we can use to create multiple objects with a slightly different features. Let's see what's that look like:
<?php
class User {
public $name;
public function __construct()
{
var_dump('This method is executed');
}
}
$user = new User();
Now our class is a little bit different, we a added a new special method called __construct()
with the double underscore. Notice that I call it method not function. So, you may know that when we create a regular function, we just call it a function. But within a context of a class we referred them as a methods, methods of an object.So, what does that
__construct()
method do?Well, it is a special method that will be called immediately when you instantiate a new class. Think of it like this, whenever you say
new User()
, the content of that __construct()
method will be automatically executed.Running
php User.php
again, the output should look like this:
string(23) "This method is executed"
Also when
__construct()
method is called, it would receive an optional argument that you pass when you instantiate the class:
<?php
class User {
public $name;
public function __construct($name)
{
$this->name = $name;
}
}
$user = new User('John Doe');
var_dump($user->name);
As you can see, we are passing an argument when we instantiate the class ($user = new User('John Doe');
), therefore the value of the $name
argument in the __construct($name)
method will be equal to John Doe
, which we assign it to the name
property using the pseudo-variable variable $this
($this->name = $name;
). Think of $this
as a way of referring to "this object" or more specifically "this instance of the class". So when you want to access property or method within the class you should use the $this->
syntax.So, when we run the code again, the output should look like this:
string(8) "John Doe"
Now, remember when I mentioned that you can have multiple objects using one class? Well, lets take a look on how we can achieve that:
<?php
class User {
public $name;
public function __construct($name)
{
$this->name = $name;
}
}
$user = new User('John Doe');
$user2 = new User('Jane Doe');
var_dump($user->name);
var_dump($user2->name);
By simply creating a new object, in this case the $user2
object, we're eliminating the problem of repeating our selves over and over, because we have now a class that can be use anywhere in our application.After running this code, the output should look like this:
string(8) "John Doe"
string(8) "Jane Doe"
Conclusion
I hope this first episode of our Object-Oriented PHP series will make you think that OOP is not as difficult as you think it is. I know there still a lot to understand, like encapsulation, inheritance, polymorphism and so on and so forth. But, if you walk through each of the episode in this series, I think you will find that it's not the most complicated thing in the world. So, keep playing around with this, and see you in the next episode.Next: Getters and Setters
Post a Comment