Hướng dẫn what is void return type in php? - void return type trong php là gì?
rfc:void_return_type Show
PHP RFC: Void Return Type
Introduction The
Return Types RFC has introduced return types to PHP. While there is already a means to specify that any value may be returned (by omitting the return type), there is no way to specify that no value should be returned, unlike many other languages with return types. This is unfortunate, as this can be useful for
documentation and error-checking purposes. In particular, it makes it clear that a function performs an action, rather than producing a result. This RFC proposes the introduction of a Proposal Support for a new function should_return_nothing(): void { return 1; // Fatal error: A void function must not return a value } Unlike other return types which are enforced when a function is called, this type is checked at compile-time, which means that an error is produced without the function needing to be called. A function with a function lacks_return(): void { // valid } function returns_nothing(): void { return; // valid } A void function may not return a value: function returns_one(): void { return 1; // Fatal error: A void function must not return a value } function returns_null(): void { return null; // Fatal error: A void function must not return a value } Note that function foobar(void $foo) { // Fatal error: void cannot be used as a parameter type } A class Foo { public function bar(): void { } } class Foobar extends Foo { public function bar(): array { // Fatal error: Declaration of Foobar::bar() must be compatible with Foo::bar(): void } } RationaleWhy check at compile-time?Generally-speaking, you should warn of problems sooner rather than later. It just so happens that it's trivial (a three-line change) to make PHP to check this at compile-time rather than run-time. So, this proposal suggests just that. Why isn't ''return null;'' permitted?Some people have asked about this, since function lacks_return(): void { // valid }3 and function lacks_return(): void { // valid }4 are technically equivalent in PHP; when a return value isn't specified, PHP will produce function lacks_return(): void { // valid }5 for you. However, choosing one over the other suggests intent. If you specify a value, it suggests the value is significant. In a void function, the return value is insignificant: it's always the same and has no actual usefulness. Specifying it explicitly with function lacks_return(): void { // valid }4 is pointless, because it doesn't really matter what value the function is going to return. Since function lacks_return(): void { // valid }5 just as it does to any other value. Use of void functions in expressionsIn some other languages, such as C, a void function can't be used in an expression, only as a statement. Since this RFC adds a way to specify a void function to PHP's syntax, it might be expected the same restriction would now apply in PHP. However, this wouldn't match precedent. PHP has had 'void functions' of a kind since its inception, in the form of built-in functions, which are documented as “void” in the manual. Such functions can be used in expressions, unlike in C. We could change PHP's rules on void functions and disallow their use in expressions, but this would create a backwards-compatibility issue: it's not inconceivable that existing PHP code relies on being able to call built-in void functions in expressions, and plenty of code assumes that you can take the return value of an arbitrary PHP function (a callback, perhaps). Moreover, IDEs and other tools can warn the user when the return value of a void function is being used. It isn't strictly necessary for the language itself to cover this. Why call it void and not null?Some have suggested that the return type be named function lacks_return(): void { // valid }5 instead, since (as previously mentioned) PHP implicitly produces null as the result value for functions which don't explicitly return something, so void would be almost the same as just enforcing that a function returns null. Plus, void might suggest a function that can't be used in an
expression, whereas function lacks_return(): void { // valid }5 wouldn't. Also, void would be a new “type” in a sense, whereas function lacks_return(): void { // valid }5 is preëxisting. The main reason to choose function lacks_return(): void { // valid }5 is that it is the customary name to use for such a return type. We already use void rather than function lacks_return(): void { // valid }5 when documenting functions in PHP, both built-in and userland functions: PHP's function prototypes in source code (e.g.), the PHP manual (e.g.) and phpDocumentor's docblock format (see definition) all use void . In addition, Hack, a PHP-derived and -compatible language which adds its own typing system, also uses void. This is just
looking at existing PHP practice; most contemporary programming languages use void here. Some of these (C, Objective-C and C++,
Java, C#, etc.) prohibit the use of a void function in an expression, but note that others
(TypeScript, ActionScript, Swift) do allow void functions in expressions, just as PHP does, by making them implicitly return some unit type. Since void seems to be the most popular choice for such a return type, both in PHP and elsewhere, why
should we name it something different? There's no precedent for it and the name doesn't seem to have been an issue until now. Một lý do khác là Thay đổi lạc hậu không tương thích Giống như các tên được dành riêng bởi các khai báo loại vô hướng và dự trữ nhiều loại hơn trong RFC Php 7, loại trả về function returns_one(): void { return 1; // Fatal error: A void function must not return a value }4 là một tên lớp rất phổ biến, và do đó, tác động tương thích ngược phải bị hạn chế. Phiên bản PHP được đề xuấtĐiều này được đề xuất cho phiên bản nhỏ tiếp theo của PHP, hiện tại Php 7.1. Bỏ phiếuVì đây là một thay đổi ngôn ngữ, cần có đa số 2/3. Cuộc bỏ phiếu là một phiếu bầu thẳng thắn để chấp nhận RFC và hợp nhất bản vá. Bỏ phiếu bắt đầu 2015-10-29 và kết thúc 10 11 ngày sau đó vào năm 2015-11-09. Bản vá và bài kiểm traThực hiệnThay đổi
rfc/void_return_type.txt · Sửa đổi lần cuối: 2017/09/22 13:28 (Chỉnh sửa bên ngoài) Loại trả về của hàm void là gì?Một hàm void không thể trả về bất kỳ giá trị nào. Nhưng chúng ta có thể sử dụng câu lệnh trả lại. Nó chỉ ra rằng chức năng bị chấm dứt. Nó làm tăng khả năng đọc của mã.cannot return any values. But we can use the return statement. It indicates that the function is terminated. It increases the readability of code.
Void có phải là một loại dữ liệu trả về không?Các hàm void không có loại trả về, nhưng chúng có thể thực hiện các giá trị trả về., but they can do return values. |