简体   繁体   English

函数返回各种数据类型

[英]Functions returning various data types

In CakePHP there is a (quite fundamental) method called find . 在CakePHP中有一个叫做find的(非常基础的)方法。 Most of the time this method would return an array but if you pass count as the first parameter the method would return a different data type - an integer. 大多数情况下,此方法将返回一个数组,但如果您将count作为第一个参数传递,则该方法将返回不同的数据类型 - 整数。

The method is allowed to return various data types. 允许该方法返回各种数据类型。

Isn't that bad? 不是那么糟糕吗?

No. It is a flexible way. 不,这是一种灵活的方式。 In php standard functions return different types too. 在PHP标准函数中也返回不同的类型。 For example strpos() . 例如strpos() It can return integer position and boolean false. 它可以返回integer位置和boolean false。

This is by design. 这是设计的。 PHP is dynamically typed language, so you cannot guard yourself against functions that return various data types (unlike Java, where you can be sure what the function is returning). PHP是动态类型语言,因此您无法防范返回各种数据类型的函数(与Java不同,您可以确定函数返回的内容)。 That's very flexible (but also can be bad) so it's a good idea to guard against unexpected results by writing unit tests. 这是非常灵活的(但也可能是坏的)所以通过编写单元测试来防止意外结果是一个好主意。 You can see phpunit for a Unit Testing framework. 您可以看到单元测试框架的phpunit。

Scripting languages tend to use this pattern of returning multiple types depending on either type of input parameters or value of parameters. 脚本语言倾向于使用这种返回多种类型的模式,具体取决于输入参数的类型或参数的值。

It can be confusing, but as long as you have good documentation its not a problem and can be very powerful. 这可能令人困惑,但只要你有良好的文档,它就不是问题而且可以非常强大。

Strongly typed languages use function overloads to do the same thing. 强类型语言使用函数重载来做同样的事情。

It's not a bad thing as long as the behaviour is documented . 只要行为记录在案,这不是坏事。 In C/C++ per example, which is a strongly-typed language, you can achieve a similar result by using void pointers ( void* ): 在C / C ++中,每个例子都是一种强类型语言,你可以通过使用void指针( void* )来实现类似的结果:

// returns a "const char*" if c == 0
// returns a "string*" if c == 1
const void* foo(int c) {
    const void* a;
    if (c == 0) {
        a = (const void*)"foo";
    } else if (c == 1) {
        string* b = new string("bar");
        a = (const void*)b;
    }
    return a;
}

You can then do this: 然后你可以这样做:

const char* x = (const char*)foo(0); // get a const char*
string* y = (string*)foo(1);         // get a string*
std::cout << x << *y;                // prints "foobar"

However, not documenting it properly may result in unexpected behaviour . 但是,未正确记录可能会导致意外行为 Because if the pointer was to be cast as an unexpected type and then used: 因为如果要将指针强制转换为意外类型,然后使用:

string* y = (string*)foo(0);         // get a const char*, bad type cast
std::cout << *y;                     // Segmentation fault

The above compiles fine but fails at runtime. 上面的编译很好,但在运行时失败。 Of course, doing a similar thing in PHP will not segfault, but will most likely screw up the rest of your program. 当然,在PHP中执行类似的操作不会出现段错误,但很可能会破坏程序的其余部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM