Beware of the underscore
Thursday, September 25th, 2008Have you ever tried using underscores (_) in your controllers variables that you are passing to the view using a combination of Controller::set() and compact()? Well, if you did, you must have realized that they never make it there, right? I haven’t submitted it as a bug yet but here it is:
class SandboxController extends AppController {
var $uses = null;
var $layout = 'sandbox';
function beforeRender(){}
function index()
{
$foo = 'foo';
$bar = 'bar';
$foo_bar = 'foobar';
$this->set(compact('foo', 'bar', 'foo_bar'));
$john = 'john';
$doe = 'doe';
$john_doe = 'john_doe';
$this->set(compact('john', 'doe'));
$this->set('john_doe', $john_doe);
}
}
Now, in your view:
echo $foo . "<br />";
echo $bar ."<br />";
echo $foo_bar ."<br />";
echo "<br /><br />";
echo $john ."<br />";
echo $doe ."<br />";
echo $john_doe ."<br />";
Finally, the returned output:

Anyone knows if that’s how it’s supposed to act?
Update: there is a way to actually get those variables intact. Thanks to Taylor for pointing it out.
$this->set(compact($foo, $bar, $foo_bar), false);