Skip to main content

Command Palette

Search for a command to run...

1. Classes and Objectes.

Published
•1 min read•View as Markdown
1. Classes and Objectes.
C

I am a Senior Web Developer | Software Engineer (Laravel, WordPress) who is experienced for more than 5 years.

  • Classes are a blueprint for creating objects. They define properties(attributes) and methods(functions) that objects of classes will have.

  • Objects are instances of classes. its created using the ‘new’ keyword.

A constructor is a special method within a class that is automatically called when an object of that class is created. Its main purpose is to initialize the object's properties or run any tasks needed before the object can be used in PHP, the constructor method is defined like this " __construct()"

<?php

class Car {
    //*** Properties*/
    public $brand;
    public $model;
    public $year;

    //*** Constructor method*/
    public function __construct($brand, $model, $year) {
        $this->brand = $brand;
        $this->model = $model;
        $this->year = $year;
    }

    //*** Method */
    public function getInfo() {
        return $this->brand . ' ' . $this->model . ' (' . $this->year . ')';
    }
}

//*** Create an instance of the Car class */
$car1 = new Car('BMW', 'M3', 2022);
$car2 = new Car('Honda', 'Civic', 2021);

//*** Accessing object properties */
echo $car1->brand; //*** BMW M3 (2022)*/
echo $car2->getInfo(); //*** Honda Civic (2021)  */  
?>

What is the OOP ?

Part 1 of 1

What is the OOP ? OOP is Object Orient Programming which helps to organize code into reusable and self-contained modules, it’s makes it easy to manage and modify, understand. Let’s look at the key concept of OOP.