Req #75263 [Com]: function __construct(): void

From: Date: Tue, 20 Apr 2021 10:39:36 +0000
Subject: Req #75263 [Com]: function __construct(): void
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-233518@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75263&edit=1

 ID:                 75263
 Comment by:         somemail at gmail dot com
 Reported by:        saymongcsw at gmail dot com
 Summary:            function __construct(): void
 Status:             Not a bug
 Type:               Feature/Change Request
 Package:            Class/Object related
 Operating System:   any
 PHP Version:        7.1.10RC1
 Block user comment: N
 Private report:     N

 New Comment:

What is the point of throwing a fatal error when declaring the correct void return type for
__construct() and __destruct() and __clone() ?
Those functions do not return a data type, but they DO return to the place from where they have been
called, don't they?

And if a fatal error should be throwed, then why isn't there a fatal error for other magic
methods, like __unserialize() and __wakeup() ?


Previous Comments:
------------------------------------------------------------------------
[2019-06-26 05:10:04] elliseprod at gmail dot com

Actually, watch the PHP doc : https://www.php.net/manual/en/language.oop5.decon.php

__construct ([ mixed $args = "" [, $... ]] ) : void

So, I agree with having the opportunity to declare VOID return type in constructor

------------------------------------------------------------------------
[2018-01-03 16:45:23] Andy_Schmidt at HM-Software dot com

Since the constructor must use 

   return 'somevalue'; 

to return a value, I consequently do believe that declaring:

   function __construct(): void { ... }

would actually be the proper way to document that restriction - now that "void" is a valid
function return type?

------------------------------------------------------------------------
[2017-09-27 05:22:41] requinix@php.net

Constructors in object-oriented languages do not have return types.

------------------------------------------------------------------------
[2017-09-26 22:54:51] spam2 at rhsoft dot net

but to gain what?

it's a magic function with special behavior anyways (see new my_class(1, 2) which calls
__constrcut without naming it)

------------------------------------------------------------------------
[2017-09-26 21:48:50] saymongcsw at gmail dot com

I mean add possibility for :void in constructor without behavior change.
Constructor would be good for configure object but without constructors in PHP works too...
(https://3v4l.org/QnvEi)
also see link in comment above that show in hhvm-3.21.0 works as expected.

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


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=75263


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


Thread (8 messages)

« previous php.bugs (#233518) next »