About the behavior of userland functions and internal functions in PHP

Asked

Viewed 164 times

3

There is a proposal to unify the behavior of userland and internal functions in the PHP 8. That currently in particular, when internal functions fail to parse argument types correctly, they fail to return null. The functions of Userland launch a Typeerror.

Regarding what would be the best behavior, I believe that launch a Typeerror, would be the best behavior.

One could exemplify. An explanation in code of such behaviors currently. Also, if possible, about this such unification.

1 answer

3


This proposal has already been approved and implemented and will come in PHP 8, according to this rfc.

With it, invalid arguments passed for internal functions will always result in a TypeError instead of a Warning in return null.

This first behavior already happens with some internal functions in PHP 7 if your code is using strict_types, as explained in a paragraph of rfc:

var_dump(strlen(new stdClass));
// Warning: strlen() expects parameter 1 to be string, object given
// NULL

declare(strict_types=1);
var_dump(strlen(new stdClass));
// TypeError: strlen() expects parameter 1 to be string, object given
  • Thank you. I read and found this ex. by the link. "For user-defined functions, passing an illegal type parameter results in a Typeerror. For internal functions, the behavior depends on several factors, but the default is to launch a warning and return null. This RFC consistently proposes to generate Typeerror exceptions for all types of invalid parameters, regardless of whether the function is defined by the user or by extension." function foo(int $bar) {}
foo("not an int"); 
// TypeError: Argument 1 passed to foo() must be of the type int, string given

Browser other questions tagged

You are not signed in. Login or sign up in order to post.