开发者

PHP code design, structure, pattern ..? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 4 years ago.

Improve this question

I want to get some input from experienced PHP developers on how to structure code.

I feel my applications soon get "dirty" and unorganized.

Should I start coding in OO? Should I start using MVC? What about design patterns? What other开发者_StackOverflow options are there? How can I organize and structure my application code better (beyond proper indentation and naming)?

Links to resources are welcomed :)

If I decide to start coding using MVC? Should I use any frameworks or just hand code everything? What is your suggestion.


Instead of worrying about creating something from scratch, I would start by using a framework and extend it with your own widgets. Zend Framework is excellent and has a massive community of support: http://framework.zend.com/

Otherwise, the best book so far I've found about PHP design is called 'Advanced PHP Programming' by George Schlossnagle. It explains patterns and design efficiently better than anywhere else I've seen.

As per comments below, I definitely don't mean to say that you should only learn a framework. It's never good to learn only a framework, but you can learn coding techniques through other peoples' code, like in frameworks/libraries/etc. Attack your learning through every source available (forums, books, code resources) and make an educated decision as to the approach you should be using when taking on a new project.


My suggestion : use the Symfony framework at least once, the tutorial will teach you most of the best practices you need to know, and let you organize your code in a good way.


Well for me JAVA is one of the best ways to really see how OOP is done. The way it's done in php is kind of lame. I've seen a couple of well structured maven based projects and I say "WOW" every time I see them again. You should start using the MVC model of coding as it'll make things easy for you to monitor - Controller = pages's logic, Model = page's (sql) data, Viewer = page's template.
Best you could see what I'm talking about at this site http://php-html.net/tutorials/model-view-controller-in-php/ .


Take it one step at a time. Download Smarty and get familiar with it. This should give you wonderful warm fuzzy feelings about separation of concerns, and give you some insights into other things to look into.


There is no silver bullet.

It doesn't matter how you start to plan, what matters is that you plan. Start with something basic like MVC and try to find reasons why your app shouldn't be made that way. If you can't find reasons, think about what frameworks might have features you are going to need.

If MVC is going to just add a lot of extra work. Such as if you are going to be doing a lot of complex AJAX or serious multi-level caching, or other reasons. Figure out what kind of system might save you time.

With heavy caching, sometimes basing the whole structure around a cache system can work. Tap into cached strings as templates and you can now alter pre-cached items. Useful for pages that are assembled on the fly iGoogle style.

The solution will come from the problem.


I would suggest you try to work with some popular framework. This way you can read clean code and form your style. 'Yii framework' is good enogh as for me. Offcourse, it will be useful to read about design patterns not only for your php skill, but for programming skill in general.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜