php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70413 FR: support to new() keyword
Submitted: 2015-09-02 20:48 UTC Modified: 2015-09-04 00:08 UTC
From: david dot proweb at gmail dot com Assigned: cmb (profile)
Status: Wont fix Package: Scripting Engine problem
PHP Version: Next Minor Version OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-09-02 20:48 UTC] david dot proweb at gmail dot com
Description:
------------
It should works exactly like the `new`, but with some features.

    // Usage.
    new('\User');      === new User;
    new(User::class);  === new User; 
    new($user);        === new $user;

    $user = new(User::class)->find(1); === (new User)->find(1);

    // Arguments.
    new(User::class, 1, 2, 3)  === new User(1, 2, 3);

    // Maybe: use another object to copy directly the name.
    $userClass = new(User::class);
    $userNew = new($userClass);   === $userNew = new(get_class($userClass));

Signature (pseudo-code):

    function new ($object, ... $args) {
        assert(is_object($object) || is_string($object));
        assert(!is_resource($object));
 
        if (is_object($object)) {
             $objectClass = get_class($object);
             return new $objectClass(... $args);
        }       
        
        return new $object(... $args);
    }



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-09-02 21:17 UTC] cmb@php.net
-Package: PHP Language Specification +Package: Scripting Engine problem
 [2015-09-02 21:17 UTC] cmb@php.net
Frankly, I can't see any real improvement with this new syntax.
 [2015-09-02 21:33 UTC] requinix@php.net
Me neither.

Already supported because you're trying to make it harder than it really is:
  new('class_name') -> new class_name
  new(User::class) -> new User
  new($user) -> new $user
  new(any, 1, 2, 3) -> new any(1, 2, 3)

Not currently supported:
  new(expr)
  new($object)
but those are both trivially solved with a temporary variable or the small function already provided.
 [2015-09-04 00:08 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2015-09-04 00:08 UTC] cmb@php.net
> Me neither.

So I'm marking this request as WONTFIX.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 05:01:29 2024 UTC