group-icon
PHP Programming
Group for all things PHP, Coding Help, Problems and Solutions
108 Members
Join this group to post and comment.
Shruthi Pillai
Shruthi Pillai
Information Technology
10 Jan 2019

What are Anonymous Classes in php?

Please give an idea about anonymous classes in php. It is a new feature in php7

DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
4mos ago

In order to work with anonymous classes you have to work with object oriented PHP and no regular classes. Anonymous classes are specially useful for " Throw-away" classes means no functions or attributes. In short it is good for quickly generating something. In PHP7 use this keyword such as 'new class { ...}'. 

The anonymous class is defined and initialized immediately and it is only once.

Like for example, here I am comparing between normal class and anonymous class

class Login {

public function log($message){

echo $message;

}}

$login = new Login(); //instantiating and pass to whatever function is going to set up our class

$application -> set_login($login);

Similarly, for anonymous class the above code would be

$login = new class{

public function log($message){

echo $message;

}}

$application -> set_login($login);

I hope you must have understand and if you want to send arguments you can add parenthesis after class.