3

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; ?

3
  • 2
    type hinting Commented Oct 19, 2016 at 9:36
  • 1
    @Faraz There is no PHP 6. Only PHP 5 and 7. Commented Oct 19, 2016 at 10:11
  • @CharlotteDunois my bad php 7. current documentation Commented Oct 19, 2016 at 10:14

2 Answers 2

8

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Not should but have to.
If you do not pass a Container object to the constructor, it will ,throw a fatal error(Uncaught TypeError) and abort. See myprogrammingnotes.com/class-name-function-arguments.html
0

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

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.