PHP classes in a nutshell

PHP classes in a nutshell

Written by Dev Dojo on Jun 23rd, 2016 Views Report Post

If you are new to PHP you've probably heard of the term class. In this quick article we'll teach you what a class is and how you can use it.

A class is used to encapsalate functionality into an object. What this means is that we can create a file called a class that has a bunch of functionality in it (referred to sometimes as an object) and we can use the functionality anywhere in our application.

When using classes in your code it is also referred to as practicing OOP or (Object Oriented Programming). Don't worry if this gets a little too far over your head, OOP is just lingo mumbo jumbo that says our code is better organized because we are using classes and objects.

Before we go any further, let me just show you what an example class looks like:


<?php

class Ninja
{

}

In the above example we created a new class called Ninja. Great! But how would we go about using this class in our code?

Well let's say that the file above is called 'Ninja.php', we would need to include or require it in our main file and create a new Ninja like so:

<?php

require 'Ninja.php';

// Now we can create our new ninja
$ninja = new Ninja;

Awesome, right? You just created a new ninja. When creating a new class object it is also called instantiating a new object or creating a new object. You can do this with the syntax above, or some people add parenthesis to the end of the object, like so:

$ninja = new Ninja();

You can use either way to instantiate a new class. Whichever way feels better to you. After all... You are the master of your own dojo...

Moving on... Why might it be useful to use classes. Well, we can add structure and create multiple instances of our object. Before doing so, let's add some more code to our Ninja class. Let's make it look like this:


<?php

class Ninja

{

	private $name;
	private $rank;
	private $weapon;

	public function __construct($name, $rank, $weapon){
		$this->name = $name;
		$this->rank = $rank;
		$this->weapon = $weapon;
	}

}

Above we have included three variables in our class which are name, rank, and weapon. So we can store the name, rank, and weapon of each of our ninjas in our object. the next function __contstruct, is called the constructor and this is created everytime a new function is called. So, when a new ninja is created we need to pass it their name, rank, and weapon. Like so:


<?php

require "Ninja.php";

$ninja1 = new Ninja('George', 'white belt ninja', 'sword');
$ninja2 = new Ninja('Bob', 'red belt ninja', 'staff');
$ninja3 = new Ninja('Mikey', 'orange belt ninja', 'nunchucks');

Awesome... now we just created 3 ninjas. But we still don't have a way of retrieving any of the data. Let's change that by making a few more modifications to our Ninja class:

<?php

class Ninja

{

	private $name;
	private $rank;
	private $weapon;

	public function __construct($name, $rank, $weapon){
		$this->name = $name;
		$this->rank = $rank;
		$this->weapon = $weapon;
	}

	public function getName(){
		return $this->name;
	}

	public function getRank(){
		return $this->rank;
	}

	public function getWeapon(){
		return $this->weapon;
	}

	public function greeting(){

		return 'Hello, my name is ' . $this->name . ', I am a ' . $this->rank . ' and my weapon of choice is the' . $this->weapon . '.';
	}

}

Ok, above we added a couple functions or (methods) to our class. Quick note: functions are referred to as methods when used in a class. When we call a function that just returns a class variable it is referred to as a getter. For instance:


public function getName(){
	return $this->name;
}

In the function we are basically returning the name of the Ninja, or getting the name of the ninja (get it? A getter). Again that's just lingo mumbo jumbo that you can ignore if you'd like.

Ok, let's put our new class into action:


<?php

require "Ninja.php";

$ninja1 = new Ninja('George', 'white belt ninja', 'sword');
$ninja2 = new Ninja('Bob', 'red belt ninja', 'staff');
$ninja3 = new Ninja('Mikey', 'orange belt ninja', 'nunchucks');

echo 'Ninja 1 name: ' . $ninja1->getName();
echo '<br />';
echo 'Ninja 2 weapon: ' . $ninja2->getWeapon();

echo '<br />';
echo $ninja3->greeting();

So, if we run the code above our output will be the following:

Ninja 1 name: George
Ninja 2 weapon: staff
Hello, my name is Mikey, I am a orange belt ninja and my weapons of choice is the nunchucks

This is just a basic example of how classes can be leveraged and used to make your code easier to manage. There are so many more cool things that you can do with classes.

And that's PHP classes in a nutshell. Now that's classy!

Comments (0)