Interview Questions

What are the advantages of each?which would you use and why?

CakePHP Interview Questions and Answers


(Continued from previous question...)

What are the advantages of each?which would you use and why?

An advantage with first case $this->set('posts', $posts); is that it allows two different names for the view file and controller file. For example, you could write something like $this->set('postData', $posts);. Now the variable name in the view file would be $postData.

The advantage with the second approach $this->set(compact()); is easier to write, and useful especially when we are setting several variables to the view.No need to add separate line for each variable as we have with $this->set();
For example,
$this->set(compact('posts','users','reports'));

(Continued on next question...)

Other Interview Questions