DEV Community

Discussion on: Explain Meta Programming Like I'm Five

Collapse
 
vasilvestre profile image
Valentin Silvestre • Edited

Bob is 5 years old. (developer)
Bob have to draw a sunny house. (new feature)
Now imagine that Bob write "Sunny house" on a drawing sheet (comment or whatever used for meta programming).

He push THE magical button of the pencil (compile code or interpret it).

TADA ! There's now a sunny house, despite he didn't draw it, he asked to his magic stick to make it and the sunny house is done (the feature).

Bob is now 25.
He have to create a new URL. The controller have to use /blog/blog_id url, be seen as "blog_list" and the blog_id MUST BE a digital, any digital. There must be an error if the page id don't meet the requirement.
Bob use Symfony, write an annotation of one line and the job is done.
(Symfony core processed to write the code.)

Here's a real example :
class BlogController extends Controller
{
/**
* @Route("/blog/{page}", name="blog_list", requirements={"page"="\d+"})
*/
public function list($page)
{
// ...
}
}

Collapse
 
codehakase profile image
Francis Sunday

Thanks for this example, super helpful