php.net |  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
Votes:18
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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
44 - 3 = ?
Subscribe to this entry?

 
 [2007-06-29 19:12 UTC] mbaynton at gmail dot com
Description:
------------
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;
		}
	};
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-11 06:24 UTC] igorsf at yahoo dot com
+1
 [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

consider:

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

versus:

(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
+1
 [2012-03-15 18:45 UTC] pajoye@php.net
Not exactly the same but look at closure and traits support in 5.4.
 [2012-03-15 18:45 UTC] pajoye@php.net
-Package: Feature/Change Request +Package: *General Issues
 [2013-07-22 15:32 UTC] pacerier at gmail dot com
+1
 [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 
PageController:
[some_page.php]
$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] johannes@php.net
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] requinix@php.net
-Status: Open +Status: Wont fix
 [2014-11-02 00:52 UTC] requinix@php.net
anonymous_classes was declined.
 [2016-03-05 16:31 UTC] mbaynton at gmail dot com
http://php.net/manual/en/language.oop5.anonymous.php happened! Thanks!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 01:01:28 2024 UTC