'Class name before variable in PHP function argument
In lots of PHP scripts I see things such as this:
public function __construct(Container $ci) {
}
I understand what a constructor does and how to pass a variable. However I'm not sure what Container means in this example? Is this the equivalent of $ci = new Container; ?
Solution 1:[1]
No, it is just type hinting the method's $ci parameter, meaning that you should pass an argument declared as an instance of Container like so:
$cont = new Container();
$obj = new YourClass($cont);
Solution 2:[2]
For those who are looking for "Type Hinting" and using the broken links above, in PHP this is now known as Type Declaration.
https://www.php.net/manual/en/language.types.declarations.php
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rax Weber |
| Solution 2 | Uri |
