Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am new to web programming and at this time I am learning about PHP. I would like to know when do I need to use a PHP framework such as CakePHP? What are things that this and other similar PHP frameworks offer for me? And is it really important to use a framework to be a professional?

  • And can I create my own framework to provide the features I like into it?
share|improve this question
3  
When you understand what it offers, and can work around it when needed, use [xyz] framework. Frameworks are tools meant for the skilled people who know how to use them and understand the limitations of their choices. Do not, under any circumstances learn a language by studying a framework alone. That's like deliberately turning off your toes, or peripheral vision. Hint, sessions behave oddly depending on what PHP framework you use .. be careful :) – Tim Post Nov 26 '10 at 15:47

8 Answers

When you're at ease with PHP, you can start using a framework. And you should always use them :

  1. It is much, much faster than re-coding everything
  2. It makes modifications easier, like changing databases or changing views
  3. Working with other people will be easier too, as they are often used to the MVC pattern frameworks use

You can create your own framework, but I definitely wouldn't recommand it. It's complicated, there are already a lot of good frameworks out there, and it's not likely you will find a missing feature. Beside, they often provide ways to add your own features to them, without having to make a whole new one.

share|improve this answer
2  
+1 for When you're at ease with PHP, you can start using a framework. Your point 3) works well only if the frameworks are very similar. I'd add "don't create a framework before having used a few different ones heavily" – peterchen Nov 26 '10 at 14:32
4  
I absolutely disagree with the statement "you should always use them". They are not suited for every project and often end up making modifications more difficult. Utilities are far more valuable than frameworks. – NickC Nov 22 '11 at 17:31
+1 for @NickC, I couldn't agree more. Some frameworks claim to be dynamic and easy to change things around, but then you get to a certain point and you just... can't. I've whinged a lot about that sort of thing :) – Stephen Orr Jun 6 '12 at 7:59
I would agree that MVC is a very good common ground allowing other people to step into your code with less of a learning curve when used properly. But MVC is the shining star there, the framework (or lack thereof) is just the implementation. – Tim Post Jun 6 '12 at 8:43

Build a few pages without a framework... you will essentially begin writing your own framework by trial and error. After you do that, move to a framework and enjoy how much time you save. Trying to build your own framework can teach you some things, and will help you appreciate the frameworks that are already out there.

When I first got into PHP, I thought frameworks were a complicated waste of time. Now, I use CodeIgniter for even simple projects. It takes about 1 minute to get up and running you already have a ton of great libraries available to you. And, as a previous poster mentioned, most frameworks are extensible, so you can always add whatever functionality you want.

share|improve this answer
1  
But how can I make sure later on that the code in framework is exactly as I want? I mean every programmer has its own style, right? What about the framework!! – Goma Nov 26 '10 at 22:47

Like Niphra said, only start using a framework when you can use PHP without a framework (that is, access the database, send headers and content, string processing, data manipulation, etc.). As far as your questions are concerned:

  • When do I need to use a PHP framework such as CakePHP? When your project grows beyond a few PHP files; the moment you start strongly separating the logic into distinct elements and modularizing/sharing functionality (sessions, etc.) is when you should use a framework.

  • What are things that this and other similar PHP frameworks offer for me? Abstraction; instead of directly interacting with the database you can use an Object-Relational Mapping (ORM) to manage the structure and relationships between your data in the database. Most framework in many languages provide ORM's to make it easier to interact with the database layer of your application. Similarly, frameworks often separate the layers of responding to user interaction. The most common separation is the Model View Controller (MVC) paradigm, which, to be brief, abstracts database logic into the Model (an interface to the ORM, in many cases), processing requests and interacting with Models into the Controller, and the rendering of the actual HTML/PDF/image/etc. into the View. Frameworks often provide other tools such as routing (to allow for complex processing of the request URI (EG: Mapping the request "http://example.com/users/1" to the controller Users, which then looks up the user Model with the ID of 1) and an abstraction of sessions and other basic structures.

  • And is it really important to use a framework to be a professional? Not necessarily; frameworks make life easier; but you don't have to use one to be a professional. Frameworks provide handy abstraction and standardization, but they aren't a requirement in most cases.

share|improve this answer

Common from a person who spent years hacking out my own ways to talk to a database and talk to online API's and am only now starting to use frameworks, use them when you at least have an understanding of what their doing, if its possible.

Don't immediately jump for RedBean on your first time working with a database, work with the DB manually first. This way you have a thorough understanding of what your actually fetching and doing to the DB, which can really help with scalability. You also can then make your own solution when the framework your using doesn't do something you need to do.

Once you understand whats going on though, use a framework! It makes things so much easier. The crazyness with storing a query then looping over the results with mysql_fetch_assoc is all gone, reduced to a single call: $book = R::load( "book", $id ). You will be way more productive and can fix future bugs or add future features much easier.

share|improve this answer

Use a framework when you know the underlying technologies well enough to know what the framework does for you, and why that's good (or bad).

A framework is sort of a meta-tool, which allows you to work with your tools (language, database) a bit faster without having to worry about some of the details of what you're doing, which is good if you really know your tools.

What you DON'T want to have happen is that you learn the framework instead of the tools, and become canalized into a certain way of doing things, and end up not really taking a mastery of the underlying languages and concepts - it should be a tool, not a crutch.

share|improve this answer

When you find yourself doing the same thing over and over again, look around and see if there is a framework that does the same redundant stuff you would have ended up doing on your own. Don't just use a framework because of the hype. I personally like CodeIgniter and Rasmus Lerdorf's "no-framework PHP MVC framework" http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html

share|improve this answer
I've written several 'no framework frameworks' to ease the pain of adding new features to mostly static (and huge) sites that just can't be easily ported into any kind of management / publication system, so they do have their uses. But I wouldn't roll my own for a brand new project (I'd just use CI if a framework was called for). – Tim Post Jun 6 '12 at 8:36
@Tim Post There typically is never a black and white answer to this question. It all depends on the project at hand, the original post didn't give details about a particular project. – Jason Holland Jun 6 '12 at 14:48
Looks like I got a down vote from a framework zealot, nice! – Jason Holland Jun 6 '12 at 14:48
Twasn't me :) Someone might have been reacting to the link you posted without really going into detail about it though. – Tim Post Jun 6 '12 at 14:51
@Tim Post Heh! That's why I made sure to separate both of these comments. :D – Jason Holland Jun 6 '12 at 15:28

Frameworks are always good to use from the get go. It promotes code re usability, better readability and save a bit of development time.

I'm not very experienced with PHP, but I would encourage any framework usage.

share|improve this answer
Why the down votes? – Pierre Pretorius Jun 7 '12 at 7:10

I'd recommend thinking carefully why you would use framework x, y or z. Every framework does not only provide functionality, that you don't have to implement yourself, but also a way of thinking. Just look at Spring, JEE, Rails or Django. Four frameworks providing enough to stomp out a web application in a little time.

But all four promote their own way of thinking on how web apps should be built. An if that doesn't fit your needs or match your flavor, you'll have a hard time taking things further.

This has been a very radical comparison but perhaps building up a small library does fit your needs better. But knowing frameworks will definitely help you work in larger teams or finding new jobs, since that shows that you interested in software development as a whole.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.