|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #41856 support for anonymous classes
Submitted: 2007-06-29 19:12 UTC Modified: 2014-11-02 00:52 UTC
Avg. Score:4.5 ± 0.8
Reproduced:15 of 16 (93.8%)
Same Version:7 (46.7%)
Same OS:2 (13.3%)
From: mbaynton at gmail dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.2.3 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-06-29 19:12 UTC] mbaynton at gmail dot com
Just voicing my opinion that it would be nice to be able to create anonymous inner classes as in Java.

Or put another way, I wish something like the reproduce code would work. This looks much better than creating and naming a global class that has some trivial purpose, like the one in the example.

Reproduce code:
if(! is_object($this->movie))
	$this->movie = new __Object()
		public function __call($name, $args)
			return false;


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-11 06:24 UTC] igorsf at yahoo dot com
 [2010-05-07 02:10 UTC] jevon at jevon dot org
Another request for anonymous classes. Without support for anonymous classes, it can sometimes be necessary to create hundreds of named, and rarely used, classes. Anonymous classes are often used for quick instances that are created and destroyed quickly, so a performance hit is reasonable.
 [2010-09-26 19:41 UTC] david dot o dot greenberg at gmail dot com
The benefits of anonymous classes are twofold:

a) efficiency of code
b) efficiency of use of resources


$Div = new DivElement($InnerHTML = 'hello');


(new DivElement( $InnerHTML = 'hello'))->display();

Lines of unnecessary code are saved, plus memory is allocated more efficiently.

This is especially useful for objects that merely appear for a single purpose and then vanish.
 [2011-05-25 19:02 UTC] urkle at outoforder dot cc
David,  I do not believe you are understanding the request here.

I believe this is more what is being requested.

class Base {
   private $name;
   public function __construct($name) {
      $this->name = $name;
   public function test() {
      return $this->name;

$obj = new class extends Base {
     public function test() {
        return '<'.$this->name.'>';

echo $obj->test();

Basically similar to that of anonymous functions introduced in PHP 5.3
 [2011-11-09 11:34 UTC] antoniocs at gmail dot com
Would be nice to have this in Php 5.4 (or a latter version)
 [2012-03-15 18:35 UTC] david71rj at gmail dot com
 [2012-03-15 18:45 UTC]
Not exactly the same but look at closure and traits support in 5.4.
 [2012-03-15 18:45 UTC]
-Package: Feature/Change Request +Package: *General Issues
 [2013-07-22 15:32 UTC] pacerier at gmail dot com
 [2013-09-02 21:42 UTC] zatacorothx at gmail dot com
Anonymous classes in Java are convenient when used with Interfaces. In PHP, this 
could help with MVC frameworks. Say all pages had a class that implemented 
$page_controller = new PageInterface() {
  function doGet() {}
  function doPost() {}

A current workaround would be using a factory or constructor to which you pass 
required methods, and not using an interface. It works, but then you have to 
deal with missing methods in application logic where you could otherwise just 
handle an error.
 [2013-09-02 22:06 UTC]
My guess is that anonymous classes would have a good chance once a good implementation comes by. This is not completely trivial as the class_entry has to be stored in the class_table but has to be somewhat hidden and we might have to find a way to hide the information in the oparray.

As all things in PHP this needs a volunteer to do it. All contributors have sometime X to work on PHP and in time X can either discuss stuff, fix bugs or implement features ... but with some guidance this might be a nice project for a newcomer (I'd volunteer to give a hand to point in the right direction etc., while  this guarantees acceptance in no way as that would need an RFC etc.) until then we have to live with workarounds.

For the PageController interface example an (work-around) alternative is

$page_controller = [
  'get'  => function () {},
  'post' => function () {}
 [2014-11-02 00:52 UTC]
-Status: Open +Status: Wont fix
 [2014-11-02 00:52 UTC]
anonymous_classes was declined.
 [2016-03-05 16:31 UTC] mbaynton at gmail dot com happened! Thanks!
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Oct 28 07:03:34 2021 UTC