Req #41856 [Com]: support for anonymous classes

From: Date: Wed, 09 Nov 2011 11:34:50 +0000
Subject: Req #41856 [Com]: support for anonymous classes
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-164991@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=41856&edit=1

 ID:                 41856
 Comment by:         antoniocs at gmail dot com
 Reported by:        mbaynton at gmail dot com
 Summary:            support for anonymous classes
 Status:             Open
 Type:               Feature/Change Request
 Package:            Feature/Change Request
 PHP Version:        5.2.3
 Block user comment: N
 Private report:     N

 New Comment:

Would be nice to have this in Php 5.4 (or a latter version)


Previous Comments:
------------------------------------------------------------------------
[2011-05-25 19:02:47] 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

------------------------------------------------------------------------
[2010-09-26 19:41:30] 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.

------------------------------------------------------------------------
[2010-05-07 02:10:11] 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.

------------------------------------------------------------------------
[2008-11-11 06:24:37] igorsf at yahoo dot com

+1

------------------------------------------------------------------------
[2007-06-29 19:12:50] 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;
		}
	};
}



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=41856&edit=1


Thread (14 messages)

« previous php.bugs (#164991) next »