Search⌘ K
AI Features

Defining Classes

Explore how to define classes in PHP, including syntax and structure. Learn to create objects from your classes and access their properties and methods, building a foundation in object-oriented programming.

Syntax for Defining Classes

We define our own class by starting with the keyword class followed by the name you want to give to your new class.

Here’s the general syntax:

C++
class className{
//properties and methods defined
}

Example Snippet

Here’s an example snippet of a class named Shape.

PHP
<?php
class Shape{
public $sides = 0; // first property
public $name= " "; // second property
public function description(){ //first method
echo "A $this->name with $this->sides sides.";
}
}
?>

Explanation

The class ...