php - understanding the parameters passed to the array_reduce method -
i going through this online tutorial, , see below script:
<?php include_once 'renderer.php'; class page { protected $renderers; public function add($renderer) { $this->renderers[] = $renderer; } public function render() { $content = ''; $content .= "--start of page--\n"; $content .= array_reduce($this->renderers , function($output , $r){ return $output .= $r->render()."\n"; } , ''); $content .= "--end of page--\n"; return $content; } } $page = new page(); $page->add(new blogrenderer()); $page->add(new articlerenderer()); echo $page->render(); lets zoom in the call array_reduce() call,
$content .= array_reduce($this->renderers , function($output , $r){ return $output .= $r->render()."\n"; } , ''); i have 2 funcdamental questions here, there 2 parameters being passed array_reduce function , when did render() become property of $r , call $r->render() valid ? how on earth call valid ?
i have seen php manual on array_reduce method , way method gets used here baffles me, can explain , answer above questions , grateful .
thank you.
alex-z.
what there 2 parameters being passed array_reduce function?
the first parameter array 2 elements (an instance of blogrenderer() , instance of articlerenderer()).
the second parameter callback function.
when did render() become property of $r?
the callback has 2 parameters, second parameter $r holds value of current iteration. instance of blogrenderer() or articlerenderer() in classes blogrenderer() , articlerenderer() there method called render()
i hope makes sense.
Comments
Post a Comment