Re: RFC: Anonymous Classes

From: Date: Thu, 26 Sep 2013 10:15:59 +0000
Subject: Re: RFC: Anonymous Classes
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On 09/26/2013 10:28 AM, Nicolas Grekas wrote:
I think what Terence was talking about is more like this: class A { } class AProxifier {
     protected function protectedMethod() {...}
     function getAProxy()
     {
         return new class extends A { /* How do you call
AProxifier->protectedMethod() here? */ };
     }
} This is possible with anonymous functions, that's a big feature of PHP5.4. And for the same reasons might be expected here too? I don't have the answer, just raising the point. (new class ...
      protected $value;
      public function __construct($value)
      {
          $this->value = $value;
      }
      public function doSomething()
      {
          echo $this->value;
      })($val)
Btw, I can't get used to ($val) beeing at the end of the declaration. I feel it very confusing. Nicolas
If you need access to the methods in AProxifier then why does the anonymous class extend A, you should extend AProxifier as you would with any other class. class A { public function __construct(/* ctor args*/) { /* ctor statements */ } protected function protectedMethod() { /* protected method */ } public function getProxy() { return new class extends A { /* proxy stuff */ } (/* ctor args */); } } For the following reasons the syntax should remain as it is: It keeps the number of conflicts in the parser the same (%expect 3) It is consistent with anonymous function calls - args after definition ... It does not make sense to pass arguments to a constructor that might not yet be declared It is the simplest syntax to implement .. and so less margin for error, easier to maintain in the future ... Cheers Joe

Thread (55 messages)

« previous php.internals (#69346) next »