PatrickLouys / no-framework-tutorial

A small tutorial to show how to create a PHP application without a framework.
MIT License
1.53k stars 186 forks source link

Question about what goes in the dependency injector and what doesn't. #56

Closed thinsoldier closed 8 years ago

thinsoldier commented 8 years ago

If the page controller needs the custom InvalidPageException to give the right kind of error message, isn't that a dependency? So should't it be added to the dependency injector settings and the constructor of the page controller instead of using new?

thinsoldier commented 8 years ago

Also

Add this at the top of your file: use Example\Page\InvalidPageException; It is important that you don't forget this step, otherwise it will try to catch the wrong exception (it's looking in the wrong namespace) and thus will never catch it.

If you hadn't told us about this error, is there any way we could have eventually found it on our own? How?


update

If I disable Whoops and enable XDebug I can actually see:

Fatal error: Uncaught exception 'Example\Page\InvalidPageException' with message 'No page with the slug services was found' in /no-framework-tut/src/Page/FilePageReader.php on line 28

along with

Example\Page\InvalidPageException: No page with the slug services was found in /no-framework-tut/src/Page/FilePageReader.php on line 28

Whoops was only showing the 2nd message.

PatrickLouys commented 8 years ago

Not everything is a dependency. You should use new for things like value objects (think DateTime) or Exceptions in this case.

See this for reference.