The class Command

The class command allows you to declare OOP classes in PHP. You can also declare an anonymous class.

Syntax

phpclass Test {

}

Example

Let's declare a class:

php<?php
  class User {
  
  }
?>

Example

Let's declare an anonymous class:

php<?php
  $obj = new class {
    public function sayHello() {
      return 'Hello';
    }
  };
  
  echo $obj->sayHello();
?>

Code execution result:

php'Hello'

See Also

  • the get_class function,
    which returns the name of the object's class
  • the instanceof function,
    which checks if an object belongs to a class