Home » Php » PHP method signature has no parameters method call has one parameter no warning

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

Posted by: admin February 25, 2020 Leave a comment

Questions:

Hello I am trying to make a simple class that has a method with no parameters. With method call I am putting in one parameter to fail a test but the test passes. And if I run the code in browser everything runs with no warning. I am on a development environment so php should detect such violations.

This is the method signature

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

This is the call

...

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

...

The result of the above is just fine I get the detail from the logic that is in the method no warnings or errors.

Edit: If I put wrong type into signature of the method I get the proper error since I have strict_types on.

How to&Answers:

PHP doesn’t care about that because all php functions are variadic by nature. This is why func_get_args exists. Anything you hand to a php function/method is assumed valid unless otherwise explicitly defined as invalid. In other words, you must define a prototype and the call must then explicitly defy that prototype for it be considered invalid.

For example:

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

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

You get

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.