Objects in PHP: Creating and Using Objects

Rumman Ansari   Software Engineer   2024-07-09 10:06:25   5436  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

An object is a data type that stores data and information on how to process that data.

In PHP, an object must be explicitly declared.

First, we must declare a class of objects. For this, we use the class keyword. A class is a structure that can contain properties and methods:

Example Code:



<?php
class Car {
    function Car() {
        $this->model = "Model Z.10";
    }
}

// create an object
$carObj = new Car();

// show object properties
echo $carObj->model;
?>


Output:

The above code will produce the following result-


Model Z.10