Hey PHP guys... OOP question

7 points by eintnohick ↗ HN
I was wondering if you guys could explain something to me. I'm developing a new website based on CakePHP and am curious as whether it would be beneficial (faster and less code) to replace:

//Controller

$this->data['text_learn'] = $this->language->get('text_learn');

//Language

$_['text_learn'] = 'Learn About Our Website';

//View

<?php echo $text_learn; ?>

Simply with:

//View

<?php echo 'Learn About Our Website'; ?>

I'm trying hard to understand why it would be beneficial to have all that excess code when I could simply echo out the text; especially in cases where it is only used once.

13 comments

[ 3.1 ms ] story [ 35.1 ms ] thread
Well I would just pass the language model into the view - it seems the better method (and still in keeping with MVC methodology).

This is what I do with Kohana (a similar framework to cakePHP).

Umm if you have hard coded text (i.e. if that text never changes) why dont you just write it into the model? That seems perfectly fine (unless CakePHP does something screwy with views I dont know about :)) in MVC terms.

Its about maintainability. Is "learn about our website" the only thing you ever want to echo? Will it always be only in english? Might you want to echo it other places, or use it in different circumstances?

Try to think of instances where you might want to make a change that causes you to have to go and search through the code for a million different echoes spread all over the place. The 'extra code' is insurance against this eventuality.

If its a short little one shot deal, then don't sweat it.

Yes yes yes! I can't emphasize this enough. My biggest regret in 10 years of building cdbaby.com was that when I wanted to make it multi-lingual, 7 years into it, I was screwed! All of the language was spread around so many little print/echo statements in 10,000 different places.

With each one I had thought, "Oh it's just a little one shot deal." But after a few years, guess what you have thousands of?

Do it their way. Get used to it. It may take you a mere 2 seconds longer to type, but will save you minutes/hours/days later on.

It's really wonderful knowing that every single word that appears on your site is all coming from one source file. So if you do want to have it translated to Spanish or Chinese some day, you can just hand that file to a translator and voilá!

Hmmm... would it really be that hard to automatically do (most of) the extraction of text constants?
I imagined your biggest regret would be wasting all of that time trying to force the site to work on RoR and then finally relenting and switching back to PHP.

Not a flame, just a serious thought: from what I remember the RoR was worked on for a year or more - did it take longer than that to implement internationalization?

this.

another common usage is to standardize error/success/etc messages. you always want one type of error to say the same thing, and a language file is a better place to put things than using a define.

having said that, its all about maximizing efficiency. if you don't think you'll actually save that much time in the future by putting things in the language file, maybe because you'll "never" internationalize, then its probably a process worth skipping for you. you can always just go back and make the changes later. it'll just be harder and take longer to do it later, as opposed to doing it now.

Hey guys, thanks a bunch for the help. It makes better sense now. Thinking about what yall said, how do can you load a controller from a controller. I tried:

$this->children = array( 'common/search', 'account/login' );

The first one (common/search) loads fine but the second one doesn't. Am I doing this wrong? Thanks in advance!

Eek, that sounds way complicated. Glad we don't have to deal with that in RoR.
Hi Eintnohick,

It definitely would be faster to simply replace it with the 'echo' statement, but you need to decide if you might want to offer your website in different languages in the future. If internationalization is a possibility then you should keep it the way it is so that you can use different language files.

- Bryce

Man you guys are awesome!! Thanks a bunch. I figured out my controller prob so yall don't need to worry about that.
Dude, before you get too deep - use Kohana; it doesn't shackle you and it is quite powerful.

I tried Cake, Symphony, and Zend Framework - they are all quite strong but Cake and Symphony make you build your app 'their way', Zend Framework is looser than the others (more a collection of libraries) but is bloated, slow, and over-engineered.

My opinion is biased because I am a core developer.

Kohana's i18n system is pretty intuitive too. http://www.kohanaphp.com