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
}
}
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
}
class CricektMatch{
}
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.
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';
}
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
<?php
class Car {
public $color = 'White';
public $carModel = "AS202";
public function greetMessage()
{
return "This car has various modern features";
}
}
?>
Here, the public is the access modifier.