Learning PHP - start out using a framework or no?
I've noticed a lot of jobs in my area for PHP. I've never used PHP before, and figure if I can get more opportunities if I pick it up then it might be a good idea. The problem is that PHP without any framework is ugly and 99% of the time really bad code. All the tutorials and books I've seen are really lousy - it never shows any kind of good programming practice but always the quick and dirty kind of way of doing things. I'm afraid that trying to learn PHP this way will just imprint these bad practices in my head and make me waste time later trying to unlearn t开发者_Go百科hem. I've used C# in the past so I'm familiar with OOP and software design patterns and similar.
Should I be trying to learn PHP by using one of the better known frameworks for it? I've looked at CakePHP, Symfony and the Zend Framework so far; Zend seems to be the most flexible without being too constraining like Cake and Symfony (although Symfony seemed less constraining than CakePHP which is trying too hard to be Ruby on Rails), but many tutorials for Zend I've seen assume you already know PHP and want to learn to use the framework.
What would be my best opportunity for learning PHP, but learning GOOD PHP that uses real software engineering techniques instead of spaghetti code? It seems all the PHP books and resources either assume you are just using raw PHP and therefore showcase bade practices, or that you already know PHP and therefore don't even touch on parts of the language.
Getting started
If you are trying to learn PHP(no PHP experience) I guess you should first pick up an easy framework like Codeigniter. When watching there screencast you will find out the with Codeigniter you can create a simple blog in 20 minutes. I don't think you can code something like this with just plain PHP because codeigniter allready has the following solid foundation:
MVC: In my opinion this helps you to write cleanly separated code.
MVC is a software approach that separates application logic from presentation. In practice, it permits your web pages to contain minimal scripting since the presentation is separate from the PHP scripting.
Database: This module helps you with a lot your database pain.
CodeIgniter comes with a full-featured and very fast abstracted database class that supports both traditional structures and Active Record patterns. The database functions offer clear, simple syntax.
Easy: codeigniter is really easy to pick up which is a huge plus.
CodeIgniter is installed in four steps:
- Unzip the package.
- Upload the CodeIgniter folders and files to your server. Normally the index.php file will be at your root.
- Open the application/config/config.php file with a text editor and set your base URL. If you intend to use encryption or sessions, set your encryption key.
- If you intend to use a database, open the application/config/database.php file with a text editor and set your database settings.
Fast: Rasmus(PHP inventor) did benchmark a lot of the PHP frameworks(slides 24-32) out there and as you can see Codeigniter performs good compared to some other framweworks. Also I would like to notice that mosts slides from http://talks.php.net/ are really good.
Learning PHP
In the past I stumbled upon Matt Zandstra's PHP book which is really good and I think you should read it to learn PHP properly.
PHP "best" practices:
"Clean" code:
- Learn Recess PHP framework. I also really liked this framework. It will help you create rest-like applications.
- Learn TDD/PHPunit to properly test your code.
- Learn MVC.
- Learn OOP.
Performance:
- APC: If it all possible you should really install apc to speed up php. It will store the PHP opcode in memory(huge, huge boost).
- Memcached: When your database is under heavy load you also need to store your query's in memory.
Good luck!
If you're worried that learning PHP without a framework is going to imprint bad practices in your head, we've got a bigger problem. ;) I believe that with any language, you should be comfortable with the language itself before you gain familiarity with any sort of framework. As someone who's delved in many of the PHP frameworks, I can guarantee that using one will not automatically help you learn "good PHP" (it's still quite possible to write crappy code within controllers, etc.).
My advice? If you already are comfortable with OOP, then you won't have trouble picking up good OOP practices in PHP. Do that first - make sure you are comfortable with the language and its idiosyncrasies. Once you're to that point, you'll have a greater base for appreciating whatever framework you choose.
(I would advise you to not get too pumped up about the Zend Framework; yes, it is "flexible" in the sense that it's a loose collection of libraries packaged around the MVC concept, but it doesn't carry a lot of true MVC functionality and is needlessly complex in a lot of areas )
Start with vanilla, otherwise you'll be looking for ways to fix brokens with no knowledge of how things work on a basic level. PHP is free after all, so is apache and mysql, and after setting those up (it shouldn't take longer then a day even if you screw something up majorly) you can play all you want.
The best thing about php? Compile changes with a page refresh 8)
The problem is that PHP without any framework is ugly and 99% of the time really bad code.
That's not true. There is definitely lots of ugly and bad code out there, but PHP without a framework is not inherently ugly, nor is it automatically bad code. Verbose, yes, everything else no. PHP is just easy to get into. That's why it is popular and many of the people using PHP to quickly dish out a webpage have no education or concern about software engineering.
If you got a firm grip on OOP and OOD, then you will have no problem applying that knowledge to your PHP code. Just read through the PHP manual to get a headstart. You will find that some things won't work as you would expect them to work in C#, but that shouldn't be any news for you.
As for whether you should start out with a framework, I'd say no. Learn a framework for what it does. It's meant to ease development of web applications. Don't use it to learn PHP. And don't expect PHP frameworks to be marvels of clean and maintainable code either. There is good ones out there, but also lots of crap.
Bad code in all languages looks like crap. PHP just makes it easy for you to write such and provides visual clues when you have.
The general recommendation is : learn php before using frameworks.
They all are flawed so , you might learn bad practices from them. Plus, you would be placing all your eggs in single basket - bad long term strategy, especially if you are gonna work in php side of development.
When you have learned php, try to make your own php framework. This is almost as a rite of passage. You cannot call yourself a "php developer" with a straight face if you have not written at least 3 of them. That would give you at least some understanding of what you want in a php framework.
Oh .. and stay away from framework CakePHP and CodeIgniter which others will no doubt advertise. They are horrible, filled with bad practices. If you must choose a framework, then look at Symfony2 or Kohana (with huge reservations). They both have flaws , but at least they have heard about some of "best practices". Bottom line is : all php frameworks are bad. And some are worse then others.
And learn difference between classical MVC , Model2 MVC and MVP. Most of php frameworks are willfully ignoring the distinction.
So .. in conclusion, some thing you should learn about:
- writing a proper Model2 MVC
- Law of Demeter
- SOLID principles
- what is global state and why it's bad
- Dependency Injection
try to look at codeigniter. it's simple to start with it.
there is no de-facto standard of a php framework(unlike ruby and RoR)
my suggestion: try to use a framework, readying books and articles when you can't understand something
Here is my opinion:
If you want to learn php, do not start with a framework, write everything from scratch and this will improve your skills drastically because if you used a framework, you don't have to do much and there by learn much because frameworks usually provide you with a host of ready made solutions for different things.
Once you have good understanding of PHP (OOP especially), only then you should move to a framework and this time the framework will be easier for you too. You can add to or modify the framework (with your own classes, etc) itself once you have good command over PHP. In the end, I would suggest you to go for the CodeIgniter framework or Kohana (improved).
My opinion is to look at some tutorials to learn the basics of how PHP handles things differently than C# (e.g. how arrays work, how hashes work). Then once you understand that, go to a framework. Don't waste time with most other tutorials in PHP because they'll just showcase really bad coding practices (look at half of the PHP/MySQL tutorials - there's never any separation it's all just done procedural style -- you want to avoid that stuff).
Since I don't use PHP myself I can't say in favor of one framework or another. From what I've read, Symfony seems to be the "best" as far as a good mix of tools to do what you need without lots of conventional constraints a la Rails. Zend Framework, which you also mentioned, seems like a good choice but it really comes across more like J2EE for PHP; it's not a real framework but a collection of useful libraries. It might be the best one to pick up long term because it doesn't force using the entire framework like the others. So it's might be the easiest to add to your toolkit for use in all projects. For instance, if you are working on a legacy PHP project with the old procedural style of DB access you can introduce ZF's database layer to refactor it without bringing in the rest of it. You can't do that with Symfony, for instance.
CodeIgniter/Kohana (as I understand it Kohana is the evolution) have a strong group of supporters too, but I can't speak to how good it is. I know that CI kind of shoehorns MVC in to allow compatibility with the old "bad" way of doing PHP. It has a lot of supporters so it might be worth looking into, as well.
Another benefit I just thought of for ZF is that keep in mind if you do get a PHP job, it's highly doubtful that they'll be using a framework already, especially if the application in question has been around more than a few years. Chances are it will be the standard Classic ASP (I'm sure you're familiar with, or at least have heard the horror stories, of that) way of having dozens of include files and mixing logic and presentation all in PHP pages. ZF would be easier to integrate in a scenario like this than basically tossing all the old code in the bin and starting fresh with a full-stack framework. Just food for thought.
Again, I'm not a PHP guy but I would follow this general path:
- Read intermediate PHP book to learn the nuances of the language (stay away from the beginner stuff since you said you have programming experience already)
- Look at Symfony so you have a "full stack" framework under your belt
- Look at Zend Framework so you have a library of tools that you can work with for projects that are more complex than a framework lends itself to (i.e. anything more than basic CRUD screens) and/or for integration on existing projects.
Hope this helps!
精彩评论