Please share your favorite application design / design patterns for use in PHP with me. Some things I'd like to know:
- How your folders are designed
- How you use object oritentation in your PHP applications
- Do you have a standard way of dealing with CRUD, pagination, or any other common tasks?
- How do you avoid using repetitive code? What's your approach to libraries/sharing common code, etc?
- What are ways in which you can make your code more elegant?
You don't have to answer all of these, answering any or a few of these will be helpful.
The reason I'm asking this,is because I'm very tired of writing repetitive, ugly code in PHP and I want to make a small framework for my freelancing projects which will make programming easier and let me focus on the challenging/business tasks rather than form validation, pagination, and the other mundane activities which make up 80% of programming work in PHP
All opinions appreciated!
i've explained most of my PHP methodology here.
but nowadays, i just use Django everywhere i can.
I might get voted down for this, but if you really do want to write your own framework, I say go for it because you will learn a lot from the experience. The other frameworks mentioned here are great and tested and you wouldn't be making a bad decision using them, but it's your choice.
Before starting to write your framework, look at the other frameworks (at their syntax, directory structure, naming schema, design patterns, etc) and try to figure out why they did what they did and what, if anything, you would do differently. Try out a few tutorials and play with their code, make a few sample apps. If, after doing that, you don't like using them, then go ahead and start planning your framework, keeping what worked and improving what didn't.
If you do decide to roll your own, here are a few things I would recommend from my own experience:
I started out with the smarty templating engine when i first got tired of mixing code and html. After hacking for a while, I realized that writing my own framework is just duplicating work.
I've done a few projects with Joomla, which is really a CMS but it gives clients a lot of control over content.
Ultimately I've settled on using a real framework for my projects. I'm using symfony, which is inspired by Rails and is very well documented, but I've heard cakePHP and ZendFramework are also very good.
I almost feel like a broken record, but I would recommend that you take a look at some of the common frameworks for two reasons:
Speaking of using a framework that can be easily extended, I have had very positive experiences with Zend Framework. It's cohesive and yet loosely coupled structure makes it possible to quickly and easily extend any existing component and the entire framework is designed around the idea that you will need to write your own helper and plugin classes to add to its overall functionality.
I've found Zend Framework to be so completely flexible that I am running a single website as part Zend Framework MVC and part my old crappy framework and even older crappier code that I haven't gotten to rewrite yet. In fact, because during our rewrite we found one page that ran unacceptably slow using the old framework, I've switched the single page to run under the Zend Framework architecture.
To answer some of your questions, I would recommend that you look into Patterns of Enterprise Application Architecture by Martin Fowler. He provides a lot of valuable insights into how to solve a number of these common problems like how to create a database interaction layer in your application. Fowler also covers topics like MVC and Front Page Controller.
I use Zend Framework, which pretty much defines folder layout and OOP (MVC paradigm). For common tasks, such as for example pagination I use
Zend_Paginator
(my model classes implementZend_Paginator_Adapter_Interface
), for validation I useZend_Validate
classes etc. Thanks to that I can fully concentrate on business logic instead of reinventing the wheel.I have to agree with the above posters. If you're not using a framework when programming in PHP you're really programming with your hands tied behind your back. I personally recommend CodeIgniter. It's the fastest framework around, it's very easy to learn, and has a very active community. All of your questions will be answered by the framework:
CodeIgniter (or any framework for that matter) separates your logic into views, models, and controllers, each with their own folder.
CI has a pagination library, and it has 3rd party libraries like DataMapper for wrapping your CRUD calls in an object oriented way (ORM).
The seperation of the model, view, and controller make for very elegant code.
(The 2 questions I didn't answer are pretty much implied when using the framework)