PHP Classes briefly explained

Writing classes in php

What is a Class

A class is also known as an object. To better understand the concept of an object, think of it as you would a material object like a bear. Let’s pretend that we are going to have to make a bear using a class. To start you would give the animal a name. In this case, class Bear. Then you would need to add characteristics. Like the animal’s color and weight. These would be the variables declared at the beginning of the class. You are declaring them as variables as it is possible that these characteristics might change with different bears. You may have the variable $color = ‘white’ or $weight= ‘500kg’. You may also have constants, these don’t change. A constant might be something that classifies a bear as a bear, for example some Scientific classifications would be KINGDOM = ‘Animalia’, or PHYLUM = ‘Chordata’. These things are common to all bears and do not change. You would also have methods. Methods are functions that either call data about the bear or calculate something for the bear and return the values back to the caller. You may have the function getColor(), to get the color of the bear. Or getKindom() to get the kindom of the bear.

When you write a class you have the option of declaring a variable or method public or private. A public method or variable is accessible by all. A private method or variable is only accessible to methods and variables within the class. A good rule of thumb in writing classes is to try to avoid making variables public. You may need to edit your classes over the course of time and the variable may change, this would affect any application that would have been referring to the variable had it been public. Rather, try to refer to variables only using methods. This ensures that even if you change how or where you store the information, the method to get the information could be rewritten to handle the changes so the applications referring to your class would continue to work and know no different.

To create a class simply write: 
class Bear { #Any code goes here }
The following is an example of a simple class. A class can include variables (methods), functions, constants etc.
class Bear
const KINGDOM = 'A constant value';
const PHYLUM = 'Chordata';
private static $color = 'White';
private static $diet = 'Salmon and Berries';
public static function getColor()
echo self::$color;
public function getDiet()
echo self::$diet;

Important Notes:

Usually it is good practice to save each class in individual files. In this case I would save the above class in the file Bear.class.php. I like to include .class within the file name. It is not necessary but it makes it much easier to know which files are classes.

There are a lot more to using Objects than what I showed you in the simple example above. But that is all you really need to know to get started using static functions and classes.

In the future I will create more tutorials showing you how to extend objects and to call them. And explain what new ClassName(); means and how the operator -> works.

2 Comments on "PHP Classes briefly explained"

  1. As a learner of php programming, this become more valuable. nice trick of explaining the methods properties, classes and all new things of programming.


Leave a Reply

Your email address will not be published. Required fields are marked *