TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Hey PHP guys... OOP question

7 pointsby eintnohickalmost 16 years ago
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:<p>//Controller<p>$this-&#62;data['text_learn'] = $this-&#62;language-&#62;get('text_learn');<p>//Language<p>$_['text_learn'] = 'Learn About Our Website';<p>//View<p>&#60;?php echo $text_learn; ?&#62;<p>Simply with:<p>//View<p>&#60;?php echo 'Learn About Our Website'; ?&#62;<p>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.

8 comments

noonespecialalmost 16 years ago
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?<p>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.<p>If its a short little one shot deal, then don't sweat it.
评论 #673644 未加载
评论 #673683 未加载
edbalmost 16 years ago
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
评论 #673962 未加载
Ixiausalmost 16 years ago
Dude, before you get too deep - use Kohana; it doesn't shackle you and it is quite powerful.<p>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.<p>My opinion is biased because I am a core developer.<p>Kohana's i18n system is pretty intuitive too. <a href="http://www.kohanaphp.com" rel="nofollow">http://www.kohanaphp.com</a>
ErrantXalmost 16 years ago
Well I would just pass the language model into the view - it seems the better method (and still in keeping with MVC methodology).<p>This is what I do with Kohana (a similar framework to cakePHP).<p>Umm if you have hard coded text (i.e. if that text <i>never</i> changes) why <i>dont</i> 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.
eintnohickalmost 16 years ago
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:<p>$this-&#62;children = array( 'common/search', 'account/login' );<p>The first one (common/search) loads fine but the second one doesn't. Am I doing this wrong? Thanks in advance!
bmickleralmost 16 years ago
Hi Eintnohick,<p>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.<p>- Bryce
eintnohickalmost 16 years ago
Man you guys are awesome!! Thanks a bunch. I figured out my controller prob so yall don't need to worry about that.
Zarathualmost 16 years ago
Eek, that sounds way complicated. Glad we don't have to deal with that in RoR.