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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to