PHP 方法签名没有参数方法调用有一个参数没有警告

PHP method signature has no parameters method call has one parameter no warning

你好,我正在尝试制作一个简单的 class,它有一个没有参数的方法。通过方法调用,我输入了一个参数来使测试失败,但测试通过了。如果我 运行 浏览器中的所有代码 运行 都没有警告。我在开发环境中,所以 php 应该检测到此类违规行为。

这是方法签名

    public function getServiceDetail(): ServiceDetail
    {
        ...
    }

这是电话

...

$this->repository->getServiceDetail(1);

...

上面的结果很好,我从方法中的逻辑中得到了细节,没有警告或错误。

编辑:如果我在方法的签名中输入了错误的类型,我会得到正确的错误,因为我打开了 strict_types

PHP 不关心这个,因为所有 php 函数本质上都是可变的。这就是 func_get_args 存在的原因。除非另有明确定义为无效,否则您交给 php function/method 的任何内容都被假定为有效。换句话说,您必须定义一个原型,然后调用必须明确地违背该原型,因为它被认为是无效的。

例如:

function foo(Array $bar, String $baz) {
    var_dump(func_get_args());
}

foo([1], "quix", new stdclass); // is perfectly legal in php

你得到

array(3) {
  [0]=>
  array(1) {
    [0]=>
    int(1)
  }
  [1]=>
  string(4) "quix"
  [2]=>
  object(stdClass)#1 (0) {
  }
}

From the manual:

PHP has support for variable-length argument lists in user-defined functions. This is implemented using the ... token in PHP 5.6 and later, and using the func_num_args(), func_get_arg(), and func_get_args() functions in PHP 5.5 and earlier.