This is my introduction to classes in PHP. Object Oriented Programming is such a large topic that can get very complicated very quickly so stay tuned as I explore OOP in PHP!

For the first installation of OOP in PHP lets begin exploring PHP classes. With this series I am assuming you already know the basics of PHP. If you see something that you have a question about or want more explanation leave a comment and I will give you as much information as I have.

According to Wikipedia

A class is a construct that is used to define a distinct type. The class is instantiated into instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members that enable its instances to have state and behavior.

The explanation that I like to use is that the class is a blueprint for an item. The class definition outlines the properties and methods that an item has when it is created from the blueprint.

Across all OOP languages classes define methods and properties. A class method is something that the object can DO, and a class property is something that the object IS.

So if I create a basic Dog class a the dog should be able to “speak”. And the dog’s fur would be a specified “fur color”.

Code Sample:

class my_dog {
	public $fur_color;

	public function speak() {
		echo "Bark";
	}
}

Now that we have created our dog class. We need to create a dog.

$fido = new my_dog();

Once we have out new dog lets set his fur color and hear him speak!

$fido->fur_color = 'brown';
$fido->speak();

Output:

Bark

Lets take a quick moment to go through the code above. First we create the new class called “my_dog”.  We then added the property “fur_color”. We then added the “speak method”. Most of this is very straight forward. You will notice the use of the protected and public keywords that control the access rules for those methods, I’ll talk about those momentarily. Once the class is defined we create a new instance of the “my_dog” class as $dog. Then we set the  dog’s fur color as brown and we have the dog speak.

Access Modifiers

Access modifiers are the keywords that tell the class who can work with the specified class properties or methods. There are three levels of access permissions “public”, “protected” and “private.

Public
Means that anyone can use the property or method

Protected
Only classes that extend the class can use the property or method

Private
Only the class can use the property or method

Using Properties

There are basically two ways to use a property. From within the class, and from outside of the class. In the above example we set fido’s fur color from outside the class.

$fido->fur_color = 'brown';

Retrieving a property is roughly the same process.

$fur_color = $fido->fur_color;

The only difference when accessing a property from within the class is by using the $this variable rather than the class’s instance name, fido.

Consider this code

class my_cat {
	protected $sound = "Meow";
	
	public function speak() {
		echo $this->sound;
	}
}

$luna = new my_cat();
$luna->speak();
Meow

Here we are creating a new my_cat class. When we want to make the method speak for the cat we use the property sound by saying

echo $this->sound;

Conclusion

This is the most basic class construct you can make. Simply setup the class’ properties and methods to reflect what you need the class to have and do. Join me for part two where we discuss PHP’s Magic Methods as I dive ever deeper into Object Oriented Programming with PHP.