Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does the documentation not say a closure can be passed? #72

Open
mauriciomateus opened this issue Nov 3, 2017 · 2 comments
Open

Comments

@mauriciomateus
Copy link
Collaborator

Hi:

Passing a closure is extremely helpful. I would not have found out a closure can be passed unless for the video.

Even though I am building complex stuff nowadays I did not know that "$mixed value" in the docblocks can be understood as a callback. Can it?

Kind of a silly question but I'd appreciate an answer.

Thanks.

@mauriciomateus mauriciomateus changed the title Why does not the documentation say a closure can be passed? Why does the documentation not say a closure can be passed? Nov 3, 2017
@adamwathan
Copy link
Collaborator

Hey @mauriciomateus! Yeah there's definitely some tricks in the Laravel source that aren't documented like that. mixed in a docblock doesn't always mean it can accept a callback, but a surprisingly large amount of methods in Laravel can accept a callback instead of a value if it makes sense.

The way I usually find out is just having an insight, "hey I really wish this could accept a callback, I wonder if it can?", then checking the source for that function and seeing if there's any clues.

One of those clues is if Laravel calls the value() function on the argument that gets passed in, which will automatically invoke the value if it's a callback, or just return the value as is otherwise:

https://github.com/laravel/framework/blob/5.5/src/Illuminate/Support/helpers.php#L1139-L1142

Hope that helps!

@javi-dev
Copy link
Collaborator

javi-dev commented Nov 3, 2017

One of the more useful things I've learned from this course is to use the source code as documentation. Source diving used to "scare" me, ("this is not my code, I'll spend hours googling this obscure thing instead"). Watching Adam doing it so often and so naturally inspired me to do it myself, even breaking and dd()ing things in the vendor folder without a care in the world, I can always rm -rf it.

I don't want to derail the thread, but I couldn't help myself to take this opportunity to thank @adamwathan for teaching me this. It's a valuable bonus skill, unrelated to TDD (but I probably wouldn't be so adventurous I didn't have the tests backing me up).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants