Classes and objects are used to make your code more efficient and less repetitive by grouping similar tasks. A class is used to define the actions and data structure used to build objects. The objects are then built using this predefined structure.
PHP Classes
A PHP class is defined by using the class
keyword. The following is an example of a class. In defining a class, you supply the names of its properties and the code for its methods.
<?php
class People {
// These are the class properties
public $name;
public $country;
// These are the class methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
function set_country($country) {
$this->country = $country;
}
function get_country() {
return $this->country;
}
}
PHP Objects
A PHP class is useless without an object. A PHP object is an instance of a class. The data associated with an object are called its properties; the functions it uses are called methods. Objects are created using the new
keyword. Now let's create an instance of the class we created.
<?php
class People {
// These are the class properties
public $name;
public $country;
// These are the class methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
function set_country($country) {
$this->country = $country;
}
function get_country() {
return $this->country;
}
}
// new instance of the People() class
$alex = new People();
// called the methods of the class
$alex->set_name('Alexander');
$alex->set_country('UK');
// accessing the object properties
echo "Name: " . $alex->get_name();
echo "<br>";
echo "Country: " . $alex->get_country();
Exercise
Create a class named Car
with name
and year
as its properties. Create the class functions set_name()
, get_name()
, set_year()
, and get_year()
. These functions should be self-explanatory (e.g get_name gets the name and set_name sets the name of the car). Create an instance of your class with the object name, $ford
. Set its name as Ford
and year as 2021
. Print the name and year by accessing the object's properties. Print using the format : Name - Year.
<?php
<?php
class Car {
public $name;
public $year;
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
function set_year($year) {
$this->year = $year;
}
function get_year() {
return $this->year;
}
}
$ford = new Car();
$ford->set_name('Ford');
$ford->set_year('2021');
echo $ford->get_name() . " - " . $ford->get_year();
{
"test_output_contains":
{
"expected":"Ford - 2021",
"error_message":"Sorry, wrong output. Check your format."
},
"test_variable_exists":
{
"object":"$ford",
"error_message":"Have you declared <code>$ford<\/code>?"
},
"test_function_exists":
[
{
"object":"set_name",
"error_message":"Did you create <code>set_name<\/code>?"
},
{
"object":"get_name",
"error_message":"Did you create <code>get_name<\/code>?"
},
{
"object":"set_year",
"error_message":"Did you create <code>set_year<\/code>?"
},
{
"object":"get_year",
"error_message":"Did you create <code>get_year<\/code>?"
}
],
"success_message":"Good job!",
"error_message":"There is something wrong on your code."
}