TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Hey PHP guys... OOP question

7 点作者 eintnohick将近 16 年前
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 条评论

noonespecial将近 16 年前
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 未加载
edb将近 16 年前
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 未加载
Ixiaus将近 16 年前
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>
ErrantX将近 16 年前
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.
eintnohick将近 16 年前
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!
bmickler将近 16 年前
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
eintnohick将近 16 年前
Man you guys are awesome!! Thanks a bunch. I figured out my controller prob so yall don't need to worry about that.
Zarathu将近 16 年前
Eek, that sounds way complicated. Glad we don't have to deal with that in RoR.