Basic class definitions begin with the keyword class, followed by a class name, followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class.
class Car { // The code }
We call properties to the variables inside a class. Properties can accept values like strings, integers, and booleans (true/false values), like any other variable. Let's add some properties to the Car class.
class Car { public $comp; public $color = 'beige'; public $hasSunRoof = true; }
Here, public
keyword is must. Without it, PHP will throw error message. Last two property have a default value where's $comp doesn't have any default value. Besides, properties we can have methods, which we will cover bit latter. Now our class is all set. It's time we should work with these classes. If you want to access any of these properties outside of the class, you must create object of that class. You can create any number of object you want and this is how it's done -
$bmw = new Car ();
The process of creating an object is also known as instantiation
. We created the object $bmw from the class Car with the new
keyword.
You can create multiple object of that class -
$bmw = new Car (); $mercedes = new Car ();
Once we create an object, we can get its properties. For example:
echo $bmw -> color; echo $mercedes -> color;
In order to get a property, we write the object name, and then dash greater than (->), and then the property name. Note that the property name does not start with the $ sign; only the object name starts with a $. The above will give the following output -
beige beige
In order to set an object property, we use a similar approach. For example, in order to set the color to 'blue' in the bmw object:
$bmw -> color = 'blue'; $mercedes -> comp = "Mercedes Benz";
The classes most often contain functions. A function inside a class is called a method. Here we add the method hello() to the class with the prefix public.
class Car { public $comp; public $color = 'beige'; public $hasSunRoof = true; public function hello() { return "beep"; } }
We put the public
keyword in front of a method.
We take similar approach to the properties to access methods -
$mercedes = new Car (); echo $mercedes -> hello();
The function above will return beep
// Declare the class class Car { // properties public $comp; public $color = 'beige'; public $hasSunRoof = true; // method that says hello public function hello() { return "beep"; } } // Create an instance $bmw = new Car (); $mercedes = new Car (); // Get the values echo $bmw -> color; // beige echo "<br />"; echo $mercedes -> color; // beige echo "<hr />"; // Set the values $bmw -> color = 'blue'; $bmw -> comp = "BMW"; $mercedes -> comp = "Mercedes Benz"; // Get the values again echo $bmw -> color; // blue echo "<br />"; echo $mercedes -> color; // beige echo "<br />"; echo $bmw -> comp; // BMW echo "<br />"; echo $mercedes -> comp; // Mercedes Benz echo "<hr />"; // Use the methods to get a beep echo $bmw -> hello(); // beep echo "<br />"; echo $mercedes -> hello(); // beep
We use this
keyword to access properties from the method inside the class.
class Car { public $comp; public $color = 'beige'; public $hasSunRoof = true; public function setColor($color) { $this->color = $color; } public function getColor(){ return $this->color; } } $bmw = new Car(); $bmw->setColor("Red"); echo $bmw->getColor();
We are accessing color using $this->color
. The above code will output "Red";
So, this is very basic of Class in PHP, in the follow up chapter we will cover Method Overloading, Method Overriding, Inheritance, Access Modifier etc that is related to the Class.