Req #71419 [NEW]: Allow ::new() syntax.

From: Date: Wed, 20 Jan 2016 13:06:09 +0000
Subject: Req #71419 [NEW]: Allow ::new() syntax.
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198794@lists.php.net to get a copy of this message
From:             andreas at dqxtech dot net
Operating system: 
PHP version:      7.0.2
Package:          Class/Object related
Bug Type:         Feature/Change Request
Bug description:Allow ::new() syntax.

Description:
------------
I often create classes that have a normal constructor, and one or more
static factory methods.

On instantiation, I need to decide between "new C()" and "C::create()".
For method chaining, the "new C()" needs additional brackets, like so:
"(new C())".

Switching between the two notations can be annoying.

Proposal: Allow a syntax "C::new()", which would be equivalent to "(new
C())".

--------

Now.. when I had this idea, I was not aware that since PHP 7, and also
in hhvm, it is possible to name a method like "new()".
https://3v4l.org/Pp68v with user-defined method new().
https://3v4l.org/YvjJ9 without such a method.
This is unfortunate, and makes this request difficult, if not
impossible.

A remaining (but not really great) option would be to give every class
an "implicit" static method ::new(), but allow this method to be
overridden.

Maybe this was already discussed and discarded, and this is the reason
why a method named "new()" is now allowed. In this case I would like
this to be considered a support request. The answer could be a link to
the discussion where this was decided.

Test script:
---------------
<?php

class C {}

class D {

  /**
   * Allowed since PHP 7
   */
  static function new() {return NULL;}
}

// Call implicit method new, equivalent to "new C()";
$instance = C::new();
assert($instance instanceof C);

// Call explicit method new.
$null = D::new();
assert($null === null);


-- 
Edit bug report at https://bugs.php.net/bug.php?id=71419&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=71419&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=71419&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=71419&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=71419&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=71419&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=71419&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=71419&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=71419&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=71419&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=71419&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=71419&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=71419&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=71419&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71419&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=71419&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=71419&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=71419&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71419&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=71419&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=71419&r=mysqlcfg



Thread (11 messages)

« previous php.bugs (#198794) next »