PHP Anonymous Class

Sheeraz Gul Sep 26, 2022
PHP Anonymous Class

This tutorial educates anonymous classes in PHP and demonstrates how to create and use these classes using different code examples. We will also learn how to nest an anonymous class in PHP.

PHP Anonymous Class

As the name shows, the anonymous class is a class that doesn’t have a name. PHP 7 introduced the features of the anonymous class, and this class is meant for only one-time use.

The anonymous class is defined inside an object of that class. The anonymous can perform anything that a normal class will perform, which includes extending, implementing, and using traits; the syntax for the anonymous class is:

$Anonymous_Object=new class {
// Your code here
}

Let’s try a simple example for the anonymous class:

<?php
$Anonymous_Object=new class {
    public function Print_Delftstack(){
        echo "Hello, This is delftstack.com";
    }
};
$Anonymous_Object->Print_Delftstack();
?>

The code above will work like a standard class and call the function Print_Delftstack() from an anonymous class. See output:

Hello, This is delftstack.com

Let’s try another example in which the anonymous class extends a class and implements an interface:

<?php
class Demo_Class{
    public function Print_Delftstack1(){
        echo "This is delftstack from a parent class.<br>";
    }
}

interface Demo_Interface{
    public function Print_Delftstack2();
}

$Anonymous_Object=new class() extends Demo_Class implements Demo_Interface {
    public function Print_Delftstack2(){
        echo "This is delftstack from the parent interface; the method is implemented from Demo_Interface.";
    }
};

$Anonymous_Object->Print_Delftstack1();
$Anonymous_Object->Print_Delftstack2();
?>

The code above extends a class and implements an interface, then uses their methods with the anonymous class. See output:

This is delftstack from a parent class.
This is delftstack from the parent interface; the method is implemented from Demo_Interface.

As we can see how the anonymous class work, but how can a class work anonymously in internal use? The answer to that is PHP gives a unique name to the anonymous class; let’s try to get the name of an anonymous class:

<?php
var_dump(get_class(new class() {
    public function Print_Delftstack(){
        echo "Hello, This is delftstack.com";
    }
} ));
?>

The code above will dump the information about the given anonymous class, which is the unique name for the anonymous class; see the output:

string(46) "class@anonymousC:\Apache24\htdocs\new.php:2$5" 

Nested Anonymous Class in PHP

The anonymous can be nested inside the body of the method of another class, but it can’t access the protected and private members of the outer class. Let’s try an example:

<?php
class Demo_Class{
   public function Delftstack1(){
      return new class(){
         public function Delftstack2(){
            echo "This is delftstack two methods from the nested anonymous class.";
         }
      };
   }
}

$Demo_Object=new Demo_Class();
$Demo_Object->Delftstack1()->Delftstack2();
?>

The code above shows how to implement the anonymous class nested to a standard class. See the output:

This is delftstack two methods from the nested anonymous class.
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PHP Class