Not bad as long as the behavior is documented . In C / C ++, for an example that is a strongly typed language, you can achieve a similar result using void ( void* ) pointers:
// 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; }
Then you can do the following:
const char* x = (const char*)foo(0);
However, without documenting it correctly , unexpected behavior may occur . Because if the pointer should be distinguished as an unexpected type, then it is used:
string* y = (string*)foo(0);
The above compiles but does not execute at runtime. Of course, segfault will not do this in PHP, but it will likely ruin the rest of your program.
source share