Jan 10
Heredoc, Sit! Stay!
posted by: Player0 in php on 01 10th, 2009 | | No Comments »

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:

$time = $test ? time() : ”;

echo <<<TEMPLATE
<p>Hello, world</p>

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

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.

Nov 3
No Fallout 3 Yet
posted by: Player0 in php, projects on 11 3rd, 2008 | | No Comments »

I’m sitting here annoyed by several things.  First of all, several of the keys on my keyboard, especially backspace, have developed a case of the stickies.  Someone spilled something on this thing and it wasn’t me.  A more important annoyance is the fact that I have still not played Fallout 3, or any of the other Fallout games.  I’m quite interested to see what Bethesda hath wrought.  But alas, I am poor, for I purchased a new PC for my wife for her birthday.  Really, I just wanted her to be able to play WAR and WOW with me.  I suspect she is somehow involved in this sticky keyboard fiasco…

I have a bunch of older computer parts hanging around especially since I gutted my wife’s PC.  I decided to buuld myself a dev box which I could bring to work.  It would be nice to offload some of my work PC’s duties to a separate machine.  Primarily the hosting of dev environments.  Also since I prefer to have my work PC running Windows for various reasons, having a second box provides me with an avenue to use linux when I need.  Especially for those dev environments.

The Windows command prompt sucks absolute balls.  Yeah, there are some replacements out there.  Cygwin is barely okay as well.  Running linux is nicer.  Especially when it’s not a virtual machine eating up every available ounce of CPU, memory and disk access.

The wife’s machine donated an Opteron 146 and some Nvidia S754 motherboard.  I had a couple SCSI3 discs and a controller laying around.  A PSU here, a DVD-ROM drive there, and a stick of the world’s cheapest DDR 1G RAM and I have a pretty powerful dev box.

Linux tends to disagree with it.  First up was CentOS 5.  This actually installs and does the software RAID 1 like I would like.  But for whatever silly reason it hates the ATI 9200SE video card I’m stuck with and my MS mouse doesn’t work with it.  So I tried Ubuntu.  The mouse works but the screen goes all flickery and it’s just not installable.  So I give Gentoo it’s shot.  First time I ever tried it.  Ugh.  It’s for maniacs.  Who has this kind of spare time??  Really, I mean, I’m sure it’s awesome once you get it going but I think that the effort is just too much.  I really don’t want to do software RAID1 partitioning manually, I’m sorry.

With my options dwindling I’m left downloading Fedora Core 9.  With any luck it’s modern enough to work with all my crap out of the box so I just don’t have to fight with anything.  We’ll see in a few minutes.  I had the DVD but it won’t boot in this old drive of mine (Creative 8X DVD that’s over 9 years old now and came with a hardware MPEG2 accelerator).

At work I’m now officially an architect.  I’m not sure what that changes necessarily but it means I have to learn entirely new software deployments.  In many ways it’s like working for a completely different company.  I’m also learning Symfony.  I’m trying *real* hard not to be skeptical about it but there are things already I do not like about it.  Perhaps this is an implimentation thing but Syfony loves loading entire MySQL record sets (and corresponding objects) in to memory.  It doesn’t seem well optimized.  But I know so little about it that I can’t really speak beyond it’s seemingly high memory usage.  In many ways, I am not a believer in PHP frameworks.  PHP itself is a language designed to be used for rapid prototyping.  By forcing developers to learn and fight with something beyond PHP aren’t you slowing them down?  It certainly hasn’t done me any favors as I jump on to this project as a total noob.  However, I agree that there are many common tasks that need to be built over and over in PHP these days.  Shared sessions, i18n, db abstraction, cms. db/object reflection, release management, scheduling/queuing, etc.  If you think about it, the problem probably lays with PHP itself.  If PHP is a rapid prototyping language than it should have more built in.  It should be more API like.  If PHP is supposed to be a lower level language than it needs to be less bloaty, but perhaps better structured.

PHP is just a seriously confused language I think.