04.
Being Awesome with Type-Hints
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
8 Comments
Hey Stan!
I see that you figured out how it works :). I just wanted to add one thing: with Symfony's autowiring feature, you *can* choose to have Symfony use Reflection to automatically resolve each dependency: https://knpuniversity.com/s.... So, passing dependencies is totally explicit... unless you want opt into some magic (I particularly like autowiring).
Cheers!
Hey @mattxtlm,
Actually, type hint with interfaces makes sense when you use only those methods which are declared in that interface. If interface doesn't have methods you need, I probably need to find another in interface (abstract class or class) which declare them and which is extended by your end class. So if you can't find methods you needed in implemented interfaces or parent classes - type hint with the end class then.
Of course, it will work if you type-hinted with an interface and then pass a class which implements that interface and has some extra methods, but it will be incorrect and could cause errors in the future which are difficult to debug.
Cheers!
I have found that `transform` method actually lives in `MarkdownInterface`.
Any reason we used `MarkdownParserInterface` and renamed function to `transformMarkdown` instead, or both ways are fine?
Yo boykodev!
Ah, nice find! And nope, it really doesn't make any practice difference. If you use
MarkdownParserInterface(which is from KnpMarkdownBundle), then you're basically coupling your code to that library: we must get some instance of their interface. If you useMarkdownInterface(from the lower-level php-markdown library), in theory that's slightly better, as you're now coupling your code to only that library (so, in theory, you could use that library directly in the future without the bundle, and you wouldn't need to change this class). So, you can see the subtle difference - it's not important, but a good thing to wrap your mind around :).Cheers!
Thanks for the detailed explanation! :)
Simfony is telling me that it actually expects an instance of MarkdownParserInterface, detecting the Max instance from $this->get('markdown.parser') in the controller (as the argument for the MarkdownTransformer constructor) as incorrect:
"Catchable Fatal
Error: Argument 1 passed to
AppBundle\Services\MarkdownTransformer::__construct() must be an
instance of AppBundle\Services\MarkdownParserInterface, instance of
Knp\Bundle\MarkdownBundle\Parser\Preset\Max given..."
What did I missed?
Haha, my fault, it was a missed use statement:
use Knp\Bundle\MarkdownBundle\MarkdownParserInterface;
So MarkdownParserInterface can be properly namespaced in my service php definition.
Hey Josue,
Oh those namespaces ;) Glad you found the problem so quick by yourself, well done!
Cheers!
"Houston: no signs of life"
Start the conversation!