15

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?
1
  • 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, 2010 at 15:47

9 Answers 9

10

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.

5
  • 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, 2010 at 14:32
  • 8
    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.
    – Nicole
    Nov 22, 2011 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 :)
    – Steve Hill
    Jun 6, 2012 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, 2012 at 8:43
  • All of these a theoretical and don't work out in practice. PHP frameworks are more likely to make all of those things harder. The average application written by scratch needs about five hundred to five thousand lines of framework code. That is about a week's work average. If it's more than that you're probably either writing a library or you're making an inner platform.
    – jgmjgm
    Mar 6, 2019 at 21:40
10

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.

3
  • 2
    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, 2010 at 22:47
  • 1
    @Goma What about the language? I haven’t found one yet that does everything the way I want it to. The framework just adds something very, very similar (hopefully at a somewhat higher level of abstraction). Seriously, coding is not about “making everything look/behave/be exactly as I want.” It is about creating something that is useful, code is just the liability we burden ourselves with because the benefits outweigh the cost of having that code in the first place. Feb 4, 2014 at 5:55
  • And Now @TaylorOtwell is the creator of Laravel ...
    – mercury
    Jun 14, 2017 at 22:56
4

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.

4

After reading some of the answers, I would like to say first that you don't often have much of an option on what framework you're going to use or whether you're going to use one or not (as a professional). I don't want to intimidate you, but you will need to build enough of an understanding of PHP to allow moving from one framework to another as well using more than one framework at once.

when do I need to use a PHP framework such as CakePHP?

There isn't a universal answer to this. As a professional, you're going to be tasked to solve specific problems and this will be the deciding factor for the when's and what's of each project, especially if a client has a specific demand. But generally speaking, you will always be using some form of framework with PHP, unless you're modifying or extending a pre-existing application that has it's own framework (such as Wordpress or PHPBB).

What are things that this and other similar PHP frameworks offer for me?

As said by dirk, they offer abstraction. What this means is that they take common, multi-step procedures and try to make them as simple and streamlined as possible. In short, they try to present just-add-water solutions for common tasks.

And is it really important to use a framework to be a professional?

Generally speaking, no it is not important to use a framework to be a professional. But realistically, you won't have much of a career if you're not familiar with at least a couple common frameworks. PHP without a framework is like C++ without a std library. You don't need it, and you can actually do more without it since you'll be directly using the lower-level builing-block functionality of the language, but it will take you much longer to do the same thing.

And can I create my own framework to provide the features I like into it?

Yes. In fact, you will likely end up doing so even if unintentionally. By this I don't mean an expansive do-all framework, but more like a tool-bag of basic functions and scripts that do tasks aren't common or generic enough to be handled by the average framework. Mine is basically made up of various file system, process handling, and rex-ex tid-bits with a few different curl and stream/socket utilities.

1

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.

1

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.

1

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

5
  • 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, 2012 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.
    – programmer
    Jun 6, 2012 at 14:48
  • 1
    Looks like I got a down vote from a framework zealot, nice!
    – programmer
    Jun 6, 2012 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, 2012 at 14:51
  • @Tim Post Heh! That's why I made sure to separate both of these comments. :D
    – programmer
    Jun 6, 2012 at 15:28
-1

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.

-2

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.

1

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