PHP Class

Last Updated: July 31, 2021

How you can create a PHP class named Products.

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