Live data from Hacker News

Hey PHP guys... OOP question

news.ycombinator.com

1–10 of 14 posts

Hey PHP guys... OOP question

#1
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

Simply with:

//View

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.

Re: Hey PHP guys... OOP question

#2
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.

Re: Hey PHP guys... OOP question

#3
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.

Re: Hey PHP guys... OOP question

#4
You should use the latter but pass it through the __() function. Read the docs on I internationalisation. There is a pretty standard way of translating text in cake

Re: Hey PHP guys... OOP question

#5
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!

Re: Hey PHP guys... OOP question

#7
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

Re: Hey PHP guys... OOP question

#9

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 aga…

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á!

Re: Hey PHP guys... OOP question

#10

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 aga…

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.

Post reply on HN