I’m still using Symfony and I find some of it’s philosophy disagreable. But one of the things that urks me the most is it’s claim that alternate PHP templating syntax, such as if…endif, is a good idea.
Take listing 4-5 for example:
<p>Hello, world!</p>
<?php if ($test): ?>
<p><?php echo time(); ?></p>
<?php endif; ?>
For real? Do you really want to treat every problem the same? Do you really think this is readable? I take issue with the thought that you always have to nest PHP inside of HTML. Sometimes it makes sense to do it the other way around. I would do it this way:
<?php
$time = $test ? time() : ”;echo <<<TEMPLATE
<p>Hello, world</p>
<p>$time</p>
TEMPLATE;
In this simple example the benefit may not be completely obvious. But this shows a very clear separation of logic and template which most architects seem to ignore, even if you’re using Symfony.
The trick to it is to think of any PHP-rich output as a template and use the heredoc to wrap that output. You can only include variables and object members in it. It’s an extra step to store all the local variables needed for the heredoc but the added separation and clarity of this code is far more important.
The html p tag is an interesting choice here. My code doesn’t do exactly the same thing as the above code because I’m always rendering the second block element. But that’s really okay! Leave it there and make sure your CSS is smart enough to not render silly whitespace around empty p tags.
In a more complicated example, $time itself might actually be a whole separate template which you can then tie in to another template and so on and so forth. This format also lends itself beautifully to object oriented display.
public function output() {
echo <<<TEMPLATE
<p>{$this->time}</p>
TEMPLATE;
}
It’s just a small example but I’ve used it in many application and it really simplifies those times where you need to embed PHP in a template. Don’t use smarty. Don’t roll your own templating system. Just use what PHP gives you.
And never use more than one set of <?php ?> tags to do a PHP conditional I’m begging you. And on that subject, endif, endforeach, endwhile is ugly. If we can’t tell what kind of brace you’re closing without you telling us than YOU’RE DOING IT WRONG.
Opinions will vary wildly. But do give heredoc a chance. It’s a wildly underrated feature and it really makes dealing with output much easier.
As for Symfony itself well… I’m still making the determination about just how much I like or dislike it as a whole. I do kind of like Propel but I’m not 100% sure what Symfony itself brings to the table outside of that. It enforces an MVC model but big whoop. Any decent PHP programmer would build something in that pattern anyway. YAML config is nice and all but the implementation is sloppy and it seems to add a lot of function overhead. Caching the PHP file structure is nice though. Propel can save a little time but it certainly doesn’t do all the things I’d like it to do. And I’ve never agreed with abstracting the DB layer from the controller layer unless you ABSOLUTELY need to support multiple database products. And that’s not common.
Symfony manages to abstract enough of the baseline PHP to make it a learning curve but I can only imagine how much performance this robs from the application. Yes, I do think you can develop things much faster in this way and I think it does a lot to make PHP a bit Ruby on Rails like. But I think it’s better geared towards virgin PHP development houses that don’t really have the architecture experience that they should. I’m all for more people doing things the right way. But if you already know the right ways than being shoehorned in to a slightly constrictive, buggy, and not well documented framework is a bit of a negative experience.
And it relies too heavily on PEAR to do any of the things I’d really like out of a framework. Symfony really needs a robust library to back it up. The kind of thing any old PHP framework has. XML libaries, unit testing, queing, date manipulation, IP services, etc. I haven’t used too many frameworks in the past but I know that other PHP frameworks just ‘have more’ tied in to them.
I’m just not sure there’s enough people in the community to really give any PHP framework the attention span it deserves. Too many differences of opinion I guess.