PHP 7.0.6 Released

Anonymous classes

Support for anonymous classes was added in PHP 7. Anonymous classes are useful when simple, one-off objects need to be created.

<?php

// Pre PHP 7 code
class Logger
{
    public function 
log($msg)
    {
        echo 
$msg;
    }
}

$util->setLogger(new Logger());

// PHP 7+ code
$util->setLogger(new class {
    public function 
log($msg)
    {
        echo 
$msg;
    }
});

They can pass arguments through to their constructors, extend other classes, implement interfaces, and use traits just like a normal class can:

<?php

class SomeClass {}
interface 
SomeInterface {}
trait 
SomeTrait {}

var_dump(new class(10) extends SomeClass implements SomeInterface {
    private 
$num;

    public function 
__construct($num)
    {
        
$this->num $num;
    }

    use 
SomeTrait;
});

The above example will output:

object(class@anonymous)#1 (1) {
  ["Command line code0x104c5b612":"class@anonymous":private]=>
  int(10)
}

Nesting an anonymous class within another class does not give it access to any private or protected methods or properties of that outer class. In order to use the outer class' protected properties or methods, the anonymous class can extend the outer class. To use the private properties of the outer class in the anonymous class, they must be passed through its constructor:

<?php

class Outer
{
    private 
$prop 1;
    protected 
$prop2 2;

    protected function 
func1()
    {
        return 
3;
    }

    public function 
func2()
    {
        return new class(
$this->prop) extends Outer {
            private 
$prop3;

            public function 
__construct($prop)
            {
                
$this->prop3 $prop;
            }

            public function 
func3()
            {
                return 
$this->prop2 $this->prop3 $this->func1();
            }
        };
    }
}

echo (new 
Outer)->func2()->func3();

The above example will output:

6

User Contributed Notes

Anonymous
1 month ago
Below three examples describe anonymous class with very simple and basic but quite understandable example

<?php
// First way - anonymous class assigned directly to variable
$ano_class_obj = new class{
    public
$prop1 = 'hello';
    public
$prop2 = 754;
    const
SETT = 'some config';

    public function
getValue()
    {
       
// do some operation
       
return 'some returned value';
    }

    public function
getValueWithArgu($str)
    {
       
// do some operation
       
return 'returned value is '.$str;
    }
};

echo
"\n";

var_dump($ano_class_obj);
echo
"\n";

echo
$ano_class_obj->prop1;
echo
"\n";

echo
$ano_class_obj->prop2;
echo
"\n";

echo
$ano_class_obj::SETT;
echo
"\n";

echo
$ano_class_obj->getValue();
echo
"\n";

echo
$ano_class_obj->getValueWithArgu('OOP');
echo
"\n";

echo
"\n";

// Second way - anonymous class assigned to variable via defined function
$ano_class_obj_with_func = ano_func();

function
ano_func()
{
    return new class {
        public
$prop1 = 'hello';
        public
$prop2 = 754;
        const
SETT = 'some config';

        public function
getValue()
        {
           
// do some operation
           
return 'some returned value';
        }

        public function
getValueWithArgu($str)
        {
           
// do some operation
           
return 'returned value is '.$str;
        }
    };
}

echo
"\n";

var_dump($ano_class_obj_with_func);
echo
"\n";

echo
$ano_class_obj_with_func->prop1;
echo
"\n";

echo
$ano_class_obj_with_func->prop2;
echo
"\n";

echo
$ano_class_obj_with_func::SETT;
echo
"\n";

echo
$ano_class_obj_with_func->getValue();
echo
"\n";

echo
$ano_class_obj_with_func->getValueWithArgu('OOP');
echo
"\n";

echo
"\n";

// Third way - passing argument to anonymous class via constructors
$arg = 1; // we got it by some operation
$config = [2, false]; // we got it by some operation
$ano_class_obj_with_arg = ano_func_with_arg($arg, $config);

function
ano_func_with_arg($arg, $config)
{
    return new class(
$arg, $config) {
        public
$prop1 = 'hello';
        public
$prop2 = 754;
        public
$prop3, $config;
        const
SETT = 'some config';

        public function
__construct($arg, $config)
        {
           
$this->prop3 = $arg;
           
$this->config =$config;
        }

        public function
getValue()
        {
           
// do some operation
           
return 'some returned value';
        }

        public function
getValueWithArgu($str)
        {
           
// do some operation
           
return 'returned value is '.$str;
        }
    };
}

echo
"\n";

var_dump($ano_class_obj_with_arg);
echo
"\n";

echo
$ano_class_obj_with_arg->prop1;
echo
"\n";

echo
$ano_class_obj_with_arg->prop2;
echo
"\n";

echo
$ano_class_obj_with_arg::SETT;
echo
"\n";

echo
$ano_class_obj_with_arg->getValue();
echo
"\n";

echo
$ano_class_obj_with_arg->getValueWithArgu('OOP');
echo
"\n";

echo
"\n";
Theriault
20 days ago
Despite being anonymous, anonymous classes are named. get_declared_classes() will return anonymous classes (PHP 7.0.5).

Because they are named, an anonymous class can also be aliased (not that its useful):
<?php
$a
= new class { public $test = 123; };
class_alias(get_class($a), "Test");
$b = new Test;
echo
$b->test; // outputs 123
?>

Unlike anonymous functions, returning an anonymous class from a function will be a new instance of the SAME anonymous class each time.

<?php
function anonymous_function () {
   return function () {
      static
$shared = 0;
      echo ++
$shared;
   };
};
function
anonymous_class() {
   return new class {
      static
$shared = 0;
      public function
__construct() {
         echo ++
self::$shared;
      }
   };
}

anonymous_function(); // outputs 1
anonymous_function(); // outputs 1

anonymous_class(); // outputs 1
anonymous_class(); // outputs 2

get_class(anonymous_class()) === get_class(anonymous_class()); // TRUE
?>
sam dot wankaner at gmail dot com
1 month ago
Below two examples describe anonymous class with very simple and basic but quite understandable example

<?php
// anonymous_class.php
// PHP 7

// First way - anonymous class assigned directly to variable
$ano_class_obj = new class{
    public
$prop1 = 'hello';
    public
$prop2 = 754;
    const
SETT = 'some config';

    public function
getValue()
    {
       
// do some operation
       
return 'some returned value';
    }

    public function
getValueWithArgu($str)
    {
       
// do some operation
       
return 'returned value is '.$str;
    }
};

echo
"\n";

var_dump($ano_class_obj);
echo
"\n";

echo
$ano_class_obj->prop1;
echo
"\n";

echo
$ano_class_obj->prop2;
echo
"\n";

echo
$ano_class_obj::SETT;
echo
"\n";

echo
$ano_class_obj->getValue();
echo
"\n";

echo
$ano_class_obj->getValueWithArgu('OOP');
echo
"\n";

echo
"\n";

// Second way - anonymous class assigned to variable via defined function
$ano_class_obj_with_func = ano_func();

function
ano_func()
{
    return new class {
        public
$prop1 = 'hello';
        public
$prop2 = 754;
        const
SETT = 'some config';

        public function
getValue()
        {
           
// do some operation
           
return 'some returned value';
        }

        public function
getValueWithArgu($str)
        {
           
// do some operation
           
return 'returned value is '.$str;
        }
    };
}

echo
"\n";

var_dump($ano_class_obj_with_func);
echo
"\n";

echo
$ano_class_obj_with_func->prop1;
echo
"\n";

echo
$ano_class_obj_with_func->prop2;
echo
"\n";

echo
$ano_class_obj_with_func::SETT;
echo
"\n";

echo
$ano_class_obj_with_func->getValue();
echo
"\n";

echo
$ano_class_obj_with_func->getValueWithArgu('OOP');
echo
"\n";

echo
"\n";

// Third way - passing argument to anonymous class via constructors
$arg = 1; // we got it by some operation
$config = [2, false]; // we got it by some operation
$ano_class_obj_with_arg = ano_func_with_arg($arg, $config);

function
ano_func_with_arg($arg, $config)
{
    return new class(
$arg, $config) {
        public
$prop1 = 'hello';
        public
$prop2 = 754;
        public
$prop3, $config;
        const
SETT = 'some config';

        public function
__construct($arg, $config)
        {
           
$this->prop3 = $arg;
           
$this->config =$config;
        }

        public function
getValue()
        {
           
// do some operation
           
return 'some returned value';
        }

        public function
getValueWithArgu($str)
        {
           
// do some operation
           
return 'returned value is '.$str;
        }
    };
}

echo
"\n";

var_dump($ano_class_obj_with_arg);
echo
"\n";

echo
$ano_class_obj_with_arg->prop1;
echo
"\n";

echo
$ano_class_obj_with_arg->prop2;
echo
"\n";

echo
$ano_class_obj_with_arg::SETT;
echo
"\n";

echo
$ano_class_obj_with_arg->getValue();
echo
"\n";

echo
$ano_class_obj_with_arg->getValueWithArgu('OOP');
echo
"\n";

echo
"\n";
To Top