PHP Class

Last Updated: September 11, 2022

In this article, we will see how you can create a PHP class named Product.

Your Product class have instance variables and methods.

You can define a class with following syntax.

<?php
class Product {
  // code goes here...
}
?>

Class names are always written in UpperCamelCase.

<?php
class Product {
    // Properties
    private $name;
    private $category;

    // Methods
    function set_name($name) {
        $this->name = $name;
    }
    function get_category() {
        return $this->category;
    }

}

You can create an Object with following code

$phone = new Product();
$phone->set_name("iPnone 7");
echo $phone->get_name();

Object has two things

  • state
  • behavior
Test