Ask HN: How do I go about PHP?
Hey hackers,
I've a background in C#, Java and JavaScript but don't know a thing about PHP. Couple of questions
a) How do you recommend I should go about learning PHP? b) Any recommended Editors (I've to use Windows)? c) I spent sometime looking for frameworks and CodeIgniter seems promising. Your thoughts?
Any other tips, books, libraries, frameworks and goodies you would recommend? Any help is highly appreciated.
66 comments
[ 2.6 ms ] story [ 134 ms ] threadAs with any programming language, the best way to get started (in my opinion) is just to build something. A blog, a guestbook: something functional and useful. In the process you'll become familiar with the language fundamentals. I recommend that you at first build something using no framework (to get an idea of what sort of security measures you need to take, handling DB transactions, etc) and then followup with the framework of your choice.
I used vim/Eclipse for Java, and still use the same for PHP. There is an Eclipse build specifically for PHP, called "Eclipse PDT." There are several others out there (Komodo, Zend Studio) but PDT is free and therefore easier to try out.
I use CakePHP as a framework and find it to meet most of my needs. I have also tried symphony and the Zend framework, but neither of them were flexible enough for me. I have not tried CodeIgniter, but I have heard good things about it. There are some articles on IBM DeveloperWorks that are a good introduction to CakePHP.
Even when used that way they are a royal PITA because of the bean conventions and the fact that many of the classes in the Java API (Collections cough) don't adhere to them. Unless you use a scriptlet you can't directly call a method on an object. You reference properties like myObject.prop1 and in the background it would call the method getProp1(). This convention only works with get, set and is* methods. If you want to get the size of a list (List.size()) you're SOL since lists don't adhere to the bean naming conventions. You either have to write a wrapper class or use a scriptlet. It's some kind of cruel joke.
PHP isn't pretty, but it's got a huge library of functions, it's easy to get it running and it doesn't enforce a bunch of questionable rules in order to protect you from yourself.
For editors I highly recommend Eclipse DPT: http://www.eclipse.org/pdt/
With a Java background I would think you'll feel right at home there.
Framework wise stay tuned to (shameless plug) http://www.recessframework.org - it's a framework I've been working on that makes greater use of the OO facilities in PHP5, is RESTful, and has a straightforward ORM. Public preview release soon.
CodeIgniter is written to run in either PHP4 or 5 which means it makes less interesting use of the PHP5 object model. If you're an OO nut I suggest staying in 5 and perhaps looking at Kohana.
I think that's a pretty good way to go, actually. You want to learn PHP by tinkering with a bunch of (a) working code that (b) has been reviewed by a community of people, at least some of whom are (c) trying to exhibit some taste. The problem with the language is that various sectors of it are poorly thought out, or have been implemented several times, and the best way to avoid stumbling over them is to follow in the footsteps of some expert guides.
Joining the userbase of one or another framework will probably give you much of the same effect.
The language itself is not hard to learn at all, except for the bits that are maddeningly silly. Watch out for the array() and the way it handles keys. Find the handy online table that compares empty() and isset() and is_null() and helps you to figure out that, in many cases, none of them is the thing you really want. ;) Watch out for accidental typecasts and their effect on your logic.
As far as editors go, I use emacs, but for about 5 years I used UltraEdit and was very pleased with it.
I personally use a framework i wrote a while back, but if I were starting over I think codeignitor would be my pick
http://www.php.net/docs.php
i would suggest downloading a version of it for quick reference. it's worth learning the commonly used functions, but a big chunk of php's value (and the main criticism against it) is a namespace filled with goodies (with varying degrees of good). This means there is probably a lot of useful functionality that's used sporadically enough that it's not worth committing permanent headspace to. Rather, learn to be quick about finding the right function to use. It's usually worth checking to see if there's some strange function/module that does exactly what you're trying to do. and if there's not, you can usually find example code in the comments - though i wouldn't recommend copying that code into production. rather, use it as a starting point for exploring the problem at hand.
i'd suggest learning php by giving yourself a well defined, if simple, project to start with and attacking it the same way you've learned languages in the past. probably with some reading, a lot of doing, occasional breaking, then more reading.
b) notepad++ or homesite are my preferred IDE's
c) codeigniter, cakephp ( http://cakephp.org/ ) and the zend framework ( http://framework.zend.com/ ) are the big frameworks i'm aware of, but i'm sure there are more out there.
enjoy!
"CodeIgniter is written to be compatible with PHP 4. Although we would have loved to take advantage of the better object handling in PHP 5 since it would have simplified some things we had to find creative solutions for (looking your way, multiple inheritance), at the time of this writing PHP 5 is not in widespread use, which means we would be alienating most of our potential audience. Major OS vendors like RedHat are moving slowly to support PHP 5, and they are unlikely to do so in the short term, so we felt that it did not serve the best interests of the PHP community to write CodeIgniter in PHP 5.
Note: CodeIgniter will run on PHP 5. It simply does not take advantage of any native features that are only available in that version."
CodeIgniter does support PHP5. But you will have to turn off warnings, etc.
b) If have experiences with Java, try one of the PHP Plugins for Eclipse.
c) CodeIgniter is mostly based on PHP 4. This results in same outdated patterns, especially regarding PHP 5.3. If you want real extensibility (and highly object orientated code) try the Zend Framework. If this is a bit too much, i recommend the Yii Framework, it's lightweight and brings lots of ideas from rails to PHP without copying the rails specific approach.
- Recommend Editor: "Eclipse PDT" [1] probably has the best tools relating to PHP development, although I just use Vim for all of my PHP work.
- Bookmark the PHP online manual [2], and add their search plugin to Firefox, so you can look up standard library functions just by typing it into the search bar.
- The Zend Framework [3], more of a great library than a traditional framework, is probably the best resource for any projects your may be starting from scratch, although CodeIgniter is definitely a top contender.
Probably the best way to learn PHP itself is to read the (rather well-done) PHP manual, which includes everything from an introduction to the language, it's parsing and syntax trees, all the way up to how its object model works, and complete references for the entire function library, and all standard modules.
Read some source code from the wild. I suggest Mediawiki and Drupal.
Key points to make sure you understand:
- Arrays are the archetypal datatype in PHP, and can be used as lists, dictionaries, or a mix of the two.
- You can do just about anything just by using the standard function library. Frameworks are not necessary, contrary to popular belief.
- Objects are a mishmash of arrays, structures, and standard C++/Java objects. You can add class variables, functions, and such at runtime.
- You can do a lot of "fancy" meta-programming in PHP; you just have to know how to accomplish what you want. The PHP library helps out a lot with this.
- Everything is a template. It may be a bit more verbose than compiled templating dialects, but PHP itself is a really handy templating system in it's own right, just using <?php and ?> tags.
[1] http://www.eclipse.org/pdt/
[2] http://www.php.net/manual/en/
[3] http://framework.zend.com/
For an IDE my favorite is PHPDesigner by MPSoftware. Eclipse is pretty good too, just a bit bloated if you like something leaner.
Up vote for that bullet, specifically for PHP.
- Convert numbers to strings and vice-versa - Compare strings to numbers - Typeless versus typed comparisons (== and ===) - Array keys using '10' (a string) versus 10 (a number)
Once you understand the type inference and type coercion concepts, it should be simple to pick up how it affects your PHP code.
Most importantly, PHP will generally do what's most logical/useful/expected when it comes to these topics, so you generally don't need to think about it, but you should be aware of it in any case.
A good reference can be found in the PHP manual, which gives you tables of comparisons between different data types in different situations: http://www.php.net/manual/en/types.comparisons.php
b) a quality text editor will work just fine. when doing php stuffs, i use jedit, and have used eclipse and netbeans in the past.
c) see a
OK, maybe not exactly, but many of them suffer from second system syndrome where they try to re-implement everything php offers.
Just use php directly.
Using a framework really helps you maintain some consistency throughout your code. If you don't like what other people have made, build your own.
So the net recommendation is, get a framework that makes a lot of secure/clean decisions for you and diverge when you think they're reinventing the wheel too much.
Worth a look.
I always recommend Notepad++ plus a XAMPP installation over any form of IDE. It's simpler in the long run.
As to learning: that is a hugely personal thing. I picked it up over 4 or 5 years (still learning even now though!). If you already have a good grouding in general programming you wont have a problem :)
But with PHP learnign by doing is a good approach :) there are lots of snippet sites out there as well for you to peek through for ideas :D
CodeIgniter is very flexible and not as strict when it comes to implementing your own code and (libraries/classes/plugins). It's very simple, implements great separation (in regard of its MVC archt.) which makes it easy to understand and use. The framework has great, easy to follow documentation with great forum support and a growing community.
i say just watch one of their video tutorials of their website and you'll see how easy it is to get started on a project.
As for an IDE editor:
if you're using windows....i HIGHLY RECOMMEND PhpED (by nusphere)
I have tested every possible editor out there and this one by far is the BEST. It's fully featured in every aspect, easily customized and runs fast!.
they have definitely taken programmers needs and dumped it into this application.
it's definitely worth a try at least.
just checkout the features here.
http://www.nusphere.com/products/phped.htm
You aren't constrained by the framework. If you want to do queries in the controller, go for it.
you're best way to start in any language is to develop a small app without a framework.....great advice by him/her.
Please also note that PHP 5 has much better OO functionality than PHP 4. PHP 4 is end-of-lifed, however CodeIgniter is still compatible with PHP 4, thus limiting it in some respects.
I wouldn't focus on too much open source PHP apps, much of them are kind of sloppy-the frameworks will probably be a better code base to look at. Just like any other language you can write sloppy code, but you can also exert some effort and make a well-organized codebase.
The last thing I would recommend is to read about PHP security issues. (Disclaimer, this is my own site) http://flourishlib.com/docs/Security has an overview of security issues, however some of it is focused on using Flourish to fix issues.
b) I would recommend looking at:
Komodo (commercial, http://www.activestate.com/Products/komodo_ide/) was alright but I haven't tried the new 5.0 release. Eclipse PDT 1.x (http://eclipse.org/pdt/) was ok, but very much a pain to configure-I hear 2.0 is coming out soon.c) In terms of frameworks, here are a few of the big ones in addition to CodeIgniter, however if performance is a concern, please do check out http://paul-m-jones.com/?p=315.
http://kohanaphp.com may be of interest since it was originally based on CodeIgniter, but has been rewritten to use PHP 5 functionality.Definitely learn to use http://php.net for lots of reference. The comments on function pages can often include a good amount of insight. All functions are accessible via http://php.net/function_name.
If you are looking for less of a framework, but more of a library, I've been working on Flourish (http://flourishlib.com) for a while. I've focused a good amount on security, documentation and usability.
I haven't used Symphony in ages, but it was originally a fork of a project called Mojavi (which I used to work on). It was designed as a pure PHP5 MVC framework with a heavy emphasis on being easy to override or modify any of the components. I don't know how true it's held to that these days, but the original code base was pretty slick.
That said. I don't use either, I've got a little 300 line OOP MVC wrapper I wrote that serves my purposes. But if I were going to look at a framework today it would be Zend or Symphony.
Yes, everyone thinks the docs aren't good. They're currently pretty decent actually. Also, the community is amazing and all of it (core code, docs and community) is just getting better.
It's a really good, VERY flexible framework and most important; incredibly portable, which I think is the reason why I love it.. I haven't had a hiccup with it since the latest versions have come out (1.2 RC3?). I've also done ALOT of client work using it, rarely on my on web host. It is the Madlib to rails' Timbaland.
That being said, cakephp 1.1 was REALLY bad. I can't stress that enough. This is probably why it gets so much bad press. There's still ALOT of information written on 1.1, alot of technical articles, howtos, etc.. and an upsetting amount of reviews and performance tests based on 1.1 or early beta 1.2 releases.
I capitalize ALOT. I'm REALLY sorry. I just really like cake...
The symfony model layer is also much nicer. Cake's models are thin, making it difficult to place much logic in the model and to reuse and extend model methods. Symfony uses propel which has an excellent way of passing criteria for a db request around, making extension easier and reducing code repetition.
The symfony askeet/jobeet tutorials are well worth working through, they do a great job of showing you how to build an app.
Since first trying symfony I've found myself replicating its features and style in other php frameworks. For example I'm currently looking at if I can insert the Propel ORM used in symfony into cake to replace its model layer.
Most web hosts support PHP4 or PHP5? Any ideas on this one?
If that doesn't work, complain to your host and demand PHP5!
b) I like NuSphere PhpEd even though it isn't free. Some people like Netbeans IDE for PHP.
c) I love CodeIgniter. Very fast, MVC, some of the best user guides for any framework, just very solid all around. Go with it initially-- I know many PHP developers who got started there and have grown to be some of the most productive web coders I've ever seen.
In your case, though, you sound like an experienced programmer in other languages, so you should be fine starting with a simple CRUD app and then progressing to frameworks; any of the popular ones will do fine.
Install Doctrine (ORM) and Smarty (Templating engine) or friends and use it religiously. If you do, you can use PHP for what it's best at: Cranking out business logic at high speed.
http://www.doctrine-project.org/
http://www.smarty.net/
Every negative criticism about CakePHP is about two years old and no longer relevant.