DEV Community

Samuel K.M
Samuel K.M

Posted on

PHP Classes & Objects & Constants

Classes & Objects

Classes and objects are the two main aspects of object-oriented programming.

For example

So, a class is a template for objects, and an object is an instance of a class.

When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

Class

Let assume we have a class Person, a person has a name, age and gender.
To define a class, use curly braces.In a class, variables are called properties and functions are called methods!

<?php
class Person{ 
 //properties of the class
 public $name;
 public $age;
 public $gender;
 //methods of the class
 function setName($name){
   $this->name = $name;
 }
 function getName(){
   echo $this->name;
 }
 function setAge($age){
   $this->age = $age;
 }
 function getAge(){
   echo $this->age;
 }
 function setGender($gender){
   $this->gender = $gender;
 }
 function getGender(){
   echo $this->gender;
 }


}

$person = new Person();
$person->setName("Samuel");
$person->getName();
?>
Enter fullscreen mode Exit fullscreen mode

Objects

Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values.

Objects of a class is created using the new keyword.

<?php
$person1 = new Person()
$person1->setName('samuel')
echo "Name:" .$person1->getName()
?>
Enter fullscreen mode Exit fullscreen mode
The $this Keyword

The $this keyword refers to the current object, and is only available inside methods.

PHP - instanceof

You can use the instanceof keyword to check if an object belongs to a specific class:

<?php
$person1= new Person();
var_dump($person1 instanceof Person);
?>
Enter fullscreen mode Exit fullscreen mode
The __construct Function

A constructor allows you to initialize an object's properties upon creation of the object.
If you create a __construct() function, PHP will automatically call this function when you create an object from a class.

<?php
class Person{ 
 //properties of the class
 public $name;
 public $age;
 public $gender;
 //methods of the class
function __construct($name,$age,$gender) {
    $this->name = $name;
    $this->age = $age;
    $this->gender = $gender;
}

 function getName(){
   return $this->name;
 }
 function getAge(){
   return $this->age;
 }
 function getGender(){
   return $this->gender;
 }
}

$person = new Person("Samuel", 18, "Male");
echo $person->getName();
echo "\n";
echo $person->getAge();
echo "\n";
echo $person->getGender();
?>

Enter fullscreen mode Exit fullscreen mode
The __destruct Function

A destructor is called when the object is destructed or the script is stopped or exited.
If you create a __destruct() function, PHP will automatically call this function at the end of the script.

<?php
class Person{ 
 //properties of the class
 public $agename;
 public $age;
 public $gender;
 //methods of the class
function __construct($name,$age,$gender) {
    $this->name = $name;
    $this->age = $age;
    $this->gender = $gender;
}
function __destruct() {
  echo "Hello {$this->name}, you are a {$this->gender} who is {$this->age} old.";
}


}

$person1 = new Person("Samuel", 18, "Male");


Enter fullscreen mode Exit fullscreen mode

Class Constants

Constants cannot be changed once declared. They are useful if you need to define some constant data within a class.

A class constant is declared inside a class with the const keyword. Class constants are case-sensitive. However, it is recommended to name the constants in all uppercase letters.

We can access a constant from outside the class by using the class name followed by the scope resolution operator (::) followed by the constant name, like here:

<?php
class Name{
  const NAME = "Samuel";
}

echo Name::NAME;
?>
Enter fullscreen mode Exit fullscreen mode

Or, we can access a constant from inside the class by using the self keyword followed by the scope resolution operator (::) followed by the constant name, like here:

<?php
class Name{
  const NAME= "Samuel";
  public function greet() {
    echo "Hello ".self::NAME;
  }
}

$greetings= new Name();
$greetings->greet();
?>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)