PHP Classes

PHP classes are the blue print of properties and methods. A variable used inside the class is known as a property and the function used in class is also known as a method.

class Car{
public $carColor;//property of the class
public $modal;//property

public function getCarInfo(){

   //method of the class


How To Define PHP Class

A class is declared with the class keyword followed by the name of the class and then a pair of braces {}. All the properties and methods are defined inside the curly braces.

class Car {
  // The code


Naming Convention Of Class

  1. Capitalize the first letter of the class name.
  2. If a class contains more than one word then capitalize each of the words. This process is known as the upper camel case. For example IndianFoodMarket, IceCream, CricektMatch, etc.
class CricektMatch{

Practical Approach To Understand PHP Class

Basically, the PHP class is a blueprint that is used to create different objects using the class blueprint. In the same way, if you have a blueprint of a house then you can create more than one similar house from that blueprint but each house may have different paints, interiors, and families inside.

How to add class property ?

Class property is basically a variable that holds different values such as string, integer, boolean, etc. Let us add property inside the class.

class Car {
  public $carModel;
  public $color = 'white';

Naming Convention Of Class Property Name

  • Start the property name with a lowercase letter.
  • If the property name contains more than one word, then capitalize every word's first letter except the first word. For example $carModel, $carModelPrize, etc.

How to add methods to a class?

Methods are basically the functions. A function that is used inside the class is known as a method. Let us add a method greetMessage() to the class Car.

Source Code

class Car {
  public $color = 'White';
  public $carModel = "AS202";
  public function greetMessage() 
    return "This car has various modern features";
Try it now

Here, the public is the access modifier.

Naming Convention Of Class Method

  • Start the function name with lower case letter.
  • If a function name has more than one word then capitalize the first letter of every word except the first word. For example carModelInfo(),carMilege(),etc.

Web Tutorials

PHP Classes
Html Tutorial HTML
Javascript Tutorial JAVASCRIPT
Css Tutorial CSS
Bootstrap 5 Tutorial BOOTSTRAP 5
Bootstrap 4 Tutorial BOOTSTRAP 4