Search⌘ K
AI Features

Defining Classes

Explore how to define classes in PHP by using the class keyword, create objects with the new keyword, and access their properties and methods. This lesson builds the foundation for understanding PHP classes 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

...