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 been messing around writing my own stuff for a while now and everytime I can never get around to finishing it fully because I get stuck on something.
And then comes the part where I come to realization of whether I am doing something right.
And as such I have given up on writing my own an going with a crowd favourite: Zend.
I looked at others but it seems Zend has been around a while and they know their things.
MVC is also the way I am going forward with anything I write now.
Using Zend Framework and Doctrine, my folder structure usually looks like this:
I imagine a lot of php developers have followed a similar route to mine: small scripts -> procedural/inline-code -> possibly a look at templating -> OOP -> then a framework. I think it may be quite common for a PHP developer to have "grown up" with PHP, learning design patterns to match the features available with the current version.
MVC is the most frequently used design pattern in the popular frameworks being used today. CakePHP is my framework of choice although Symphony and Zend are very popular too – it's well worth trying out a few and it'll soon become apparent which you feel most comfortable with.
For most projects (where rapid development and portable code are the priorities) I use Cake, however for light weight apps (one I developed recently was Good Baad) that you'd like to run fast (on low spec hardware) and do not need the bulk/weight added by the functionality of one of the large frameworks I recommend reading Rasmus Lerdorf's article on his No Framework PHP MVC framework.
Basically if you're after a true object oriented language that encourages beautiful code and the best design practices PHP is always going to lose out to the likes of Ruby Python and C#. But, PHP has its strengths e.g. no need for a templating language (it is one), PHP can run very fast and cheaply and does not need the weight of a large framework for all applications.
I'd encourage adopting a design pattern that takes the manageability of a design pattern like MVC and combine it with PHP's strengths.