|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-05-08 15:37 UTC] Andy_Schmidt at HM-Software dot com
Description: ------------ According to PHP's manual: http://php.net/manual/en/language.oop5.decon.php the signature of the __destruct method is: void __destruct ( void ) Consequently, the following syntax would be correct for 7.2, now that a "void" type hint is available: function __destruct(): void {} Actual result: -------------- PHP Fatal error: Destructor __destruct() cannot declare a return type PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 15:00:02 2025 UTC |
> using a syntax that resembles that of normal methods is besides the point. Hmm, okay, poor choice of words there, but what I'm trying to say is that for these three methods return types do not make sense. Because there are no return values. Not in the sense of "this function isn't returning a value" but more "this function is incapable of using return values in the first place". Other languages: C# and Java don't have types. > class Example { public Example() { } } Python 3 does support a return type on __init__ but for reasons I don't quite understand. https://www.python.org/dev/peps/pep-0484/#id13 > (Note that the return type of __init__ ought to be annotated with -> None. The reason for this is subtle. If > __init__ assumed a return annotation of -> None, would that mean that an argument-less, un-annotated __init__ method > should still be type-checked? Rather than leaving this ambiguous or introducing an exception to the exception, we > simply say that __init__ ought to have a return annotation; the default behavior is thus the same as for other > methods.) I can't tell what ambiguity they're talking about, but I suspect it doesn't apply to PHP since return types are invariant. (There's also __new__ but that's more of a factory method than a constructor.) Meanwhile Ruby has constructors but no return types, and Perl has return types but no constructors.