Declare in PHP

Find this useful? Support us: Star on GitHub 6
Category: Method / Function | Language: PHP

To declare a class in PHP, you use the "class" keyword followed by the name of the class, which can be any valid identifier. Here's an example:

class Car {
    // properties
    public $make;
    public $model;
    
    // constructor
    public function __construct($make, $model) {
        $this->make = $make;
        $this->model = $model;
    }
    
    // methods
    public function getInfo() {
        return "This car is a {$this->make} {$this->model}.";
    }
}

In this example, we've declared a class called "Car". It has two properties, "make" and "model", which are public (meaning they can be accessed from outside the class). It also has a constructor, which accepts two parameters (for the make and model), and sets the corresponding properties of the object. Finally, it has a method called "getInfo", which returns a string describing the make and model of the car.

To create an instance of this class, you would use the "new" keyword:

$myCar = new Car("Honda", "Civic");

This creates a new Car object with the make "Honda" and model "Civic". You can then call methods on this object, like so:

echo $myCar->getInfo(); // outputs "This car is a Honda Civic."