Search code examples
phpframeworkscode-reuse

How to write resuable php framework, code?


Hello there I am new to php and want to learn to write reusable php code, a framework to build CMS for the sites that I am going to create.

From where should I learn such approach. If Possible please share some of the links of tutorials pdfs anything that can help me.


Solution

  • Like Michael said, don't re-invent the wheel.

    You can choose from many frameworks when it comes to PHP. You might also consider using Ruby on Rails. In my experience, all 'good' PHP frameworks are just Ruby on Rails wannabes and bad at that too. But saying that is just begging for a flame war, so lets all pretend I didn't just say that.

    ---- on topic ----

    You say that you are a beginning PHP-er and want to learn to 1. build reusable code and 2. build a CMS using 1.

    An important practice of writing reusable code to me is that you split up functionality of a library you make up in as many parts as you can. To take a recent example of a digital store I am building, I would have a function that clears my shopping cart, one that clears the register one that resets the fields, etc. By splitting the code up in these different functions I was able to call some of them when a transaction was complete (with the addition of saving the transaction) and I when a search was completed (just reset the fields). Think of it like a chest of tools where you can either have screwdrivers attached to all the other tools, or you could just have 1 screwdriver. And this is just one aspect of it.

    @Galwegian posted a good answer about more elements to reusable code on SO: How do you make code reusable? as well.

    1. Learning how to build a CMS is a pretty broad question. If you were doing it just for the fun of it or just so to learn how to write better PHP; look around at what other CMSes are doing and wonder how they solved certain things, what makes them work or not and try to figure out how you can build that too.

    If you want to build websites professionally; please don't create your own CMS. You will hate yourself for it later. Go for something with a wide user base and many tutorials, like drupal or wordpress. When building your own CMS you'll just be stuck wasting time finding solutions to problems that were already solved a lot better by others.

    -- half topic--- I saw this PHP course coming by, it isn't free though, but it should get you started. Nettuts is a good resource for tutorials and alike either way, check it out.

    http://net.tutsplus.com/tutorials/php/php-fundamentals-new-premium-course/