DEV Community

Naveen Dinushka
Naveen Dinushka

Posted on

How to instantiate a PHP class, write properties, methods, static properties and more

Lets write a class named "CAR"

PHP Class (car.inc.php)

class Car{
    //Properties
    private $name;
    private $bodyColor;
    private $year;

    public static $NoOfTyres = 4;


    public function __construct($name,$bodyColor,$year){
        $this->name= $name;
        $this->bodyColor = $bodyColor;
        $this->year = $year;
    }

    //Methods
    public function setName($name){
        $this->name = $name;
    }

    public function getName(){
        return $this->name;
    }
    public function getbodyColor(){
        return $this->bodyColor;
    }
    public function getYear(){
        return $this->year;
    }

    public static function setNoOfTyres($newNoTyres){
        self::$NoOfTyres = $newNoTyres;
    }


}

In the above class we have a constructor, we look at the constructor and go 'hm this car-constructor got three parameters name, bodyColor and year'. So we create the object in the index.php file, in this case - 'tesla' and pass three parameters (modelx,black,2019) . then to access them we create methods like getName, getYear and getbodyColor and in each method we return the corresponding variable. This is because the private variables/properties cannot be accessed directly .

We replace X with each property or variable.

 public function getX(){
        return $this->x;
    }

We have written a method 'setName()' which allows us to change the name of an instantiated class.

By writing following

$tesla = new Car("Modelx","black","2019");

We passed "modelx" as the name but if we decide to change it we can do the following

  $tesla->setName("ModelY");  

And it would change.

Whats up with the 'static' keyword?

The static keyword can be used for cases like 'the No Of tyres in a car'. No Of tyres in a car would not change and to access the static property No Of Tyres in the above class we can echo out like so;

echo Car::$NoOfTyres;

But down the line, say someone wanted 6 tyres in the car, so to change the static property we write another method like so

public static function setNoOfTyres($newNoTyres){
        self::$NoOfTyres = $newNoTyres;
    }

then to use it we can do


  Car::setNoOfTyres(6);

Then echo it out to find if it actually changed.

Index.php would look like this

<?php 
include 'includes/car.inc.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <?php

      $tesla = new Car("Modelx","black","2019");

      echo $tesla->getName();
      echo  "<br>";
      echo $tesla->getbodyColor();
      echo  "<br>";
      echo $tesla->getYear();

       $tesla->setName("ModelY");  
       echo  "<br>"; 
       echo  "<br>"; 
       echo $tesla->getName();  
       echo "<br>" ;
        echo Car::$NoOfTyres;
        Car::setNoOfTyres(6);
        echo "<br>";
        echo Car::$NoOfTyres;

    ?>
</body>
</html>

I have explained this in a video, see if it will help :)

Link to the video: https://youtu.be/f9jay4Ios-4

Top comments (0)