For the last 10 years I have been a .NET developer and before that I worked with C++ and MFC, so I’m pretty familiar with windows and IIS development. However, more recently I have been working almost exclusively with PHP. I have used PHP on and off for as long as I have been working with .NET but not in a professional capacity. I’ve always been disappointed with PHP compared to .NET that it didn’t have the same range of tools and features so I always felt uncomfortable and wanted to get back to Visual Studio as quickly as possible. However, things have changed, PHP has grown up and I finally feel like I have everything I need to create great apps in PHP. I’m going to talk about some of the tools and features I use in .NET and their equivalents in PHP. I won’t be discussing language features because it’s covered extensively elsewhere and I don’t know enough about it to speak with any authority. Also, this isn’t intended to be an exhaustive list, more of a starting point for .NET devs starting to work with PHP.
I love ASP.NET and more specifically ASP.NET MVC. Of course MVC isn’t exclusive to ASP so there are a number of MVC frameworks in PHP. Here’s a few to start you off.
Zend Framework – this is a heavyweight framework which includes MVC amongst many other things. It’s very enterprise focused and is quite mature. However, I find it overly complex and the learning curve is steep.
CodeIgniter – this is the one I am most familiar with having worked with the Expressionengine CMS a lot recently (which is based on CodeIgniter). This is a great framework but has been neglected recently which has seen it left behind by new entrants into the market. Its promise to support PHP 4 has also seen it held back from using some of the more modern features of PHP, alhough I believe the next version of CodeIgniter will be PHP 5 only.
FuelPHP – this is a framework which I believe was an attempt to improve on CodeIgniter by dropping PHP4 support and taking advantage of PHP 5 features. It looks like a nice framework but I haven’t really used it so can’t comment further
Laravel – this is just awesome in every way. Its tagline is ‘A Framework for Web Artisans’ – an artisan I most definitely am not – but look past that and you’ll see some truly beautiful, thoughtful and elegant code. This framework has a base requirement of PHP 5.3 which allows Laravel to take advantage of some of the great 5.3 features (such as namespaces, auto loading and closures which are similar to Lambdas). This is the framework I am currently using and I will be referring to it throughout the rest of this article because it’s great.
Routing in laravel is actually quite similar to ASP.NET so you should feel right at home. It has routing by verb (GET,POST,PUT etc), basic constraints and also has the reverse URL generation (same as URL.Action()). However, its syntax is cleaner and it has one additional feature: you can declare routes as a closure (like a Lambda Expression) and just return your views etc inline within the route definition. No need for a controller. I personally don’t use this feature but I think it’s a great idea for smaller sites or simple actions which don’t require a full controller.
IOC / Dependancy Injection
Inversion of Control (IOC) and Dependancy Injection (DI) are two really useful features of any modern language, especially for automated testing. In .NET I used StructureMap and it could do everything I needed, although with a somewhat verbose dialect and configuration management.
Laravel has a very simple IoC Container but it works great. You effectively register your type with the global container when you start up the app and then retrieve instances of your type thoughout the app simply by calling the static IoC::resolve() method. You can setup your type to be a singleton, and you can intialise it with a specific instance rather than a factory method. That’s it, and to be honest that’s enough for most apps.
ORMs in my experience are a blessing and a curse. My ORM of choice in .NET is NHibernate but I’ve also used LinQ2SQL and Entity Framework but I’m not a fan of those.
In PHP there are a few ORM options but Laravel has its own ORM called Eloquent. Of course it’s nowhere near as complex as NHibernate but that can only be a good thing. It supports all the basic CRUD features, relationships, eager loading etc and the syntax is clean and simple. You can even go further and link in Laravel’s validation routines to get automatic model validation which takes you very close to having something similar to Data Annotaions in .NET.
So I love nuget. It’s a blessing for .NET and makes it so simple to add functionality to an app from the massive range of open source libraries available.
PHP has an equivalent system called Composer and the default public package repository is called packagist. This is not as large a repository as nuget.org but it has everything you need. And if it doesn’t you can create your own.
Laravel can use composer but it also has it’s own similar proposition called bundles. These are self-registering packages which reside within your Laravel project which can inject functionality into your Laravel app. Because bundles are Laravel specific, they can integrate further than Composer even going so far as to create their own controllers, views and routes. However, it has been recognised that this is a duplication given the ubiquity of Composer and so in the upcoming Laravel 4 release bundles are to be replaced with Composer packages.
PHP development in my experience is done is either of two ways; either the old school way which is just using a text editor or using a full IDE.
So, first things first, what is the equivalent to Visual Studio for PHP? Well, there are many and in my opinion, none of them are as good as VS. But do you need VS? Probably not. There are a number of IDEs available and I’ve tried most of them, but my favourite is PHPStorm. It’s made by the geniuses at Jetbrains (who you’ll be familiar with if you use ReSharper) and has some great features (refactoring, test running, source code management, error detection) and is very actively developed. It costs money but it’s not expensive.
The other route which is popular is to use a text editor rather than an IDE and then use command line tools for running tests, source control etc. The most popular text editor at the moment seems to be Sublime Text 2 and deservedly so. It’s great. It looks beautiful, it’s fast and has some great editing features. I use it from time to time for editing single files but I prefer having an IDE so I can visualise my whole project.
I’ve used Sourcesafe and TFS in my .NET work so everything else is fantastic in comparison. There are many solutions available but really you want to use git, and more specifically GitHub.com. Git is a great source control system, but github takes it to the next level and is just amazing. Everything integrates with it and once you understand how git works you will love it.
Unit and integration testing are frankly a time consuming pain point for me. However, I love the security, code quality improvements and confidence it gives me to make changes in my code without fear of unintended consequences. In the .NET world there are a multitude of solutions but most people will use either MSTest or NUnit.
In PHP there is really only one player, and that’s PHPUnit. You’ll be pleased to know that PHPUnit is actually pretty good. It has a lot of the features you would expect (many assertation types, expected exceptions, tearup/teardown methods etc) and works pretty well for the most part. It can also integrate with PHP Code Coverage to give you some notion of coverage for our code. Also, PHPStorm integrates with both PHPUnit and PHP Code Coverage so you can run tests and see coverage directly in the IDE.
Cloud Hosting & PaaS
“The Cloud” is an annoying buzzword right now but if you are building a startup, cloud hosting is the obvious starting point. .NET has AppHarbor which is awesome, and Azure which is awesome now that Scott Guthrie is in charge, and gets better month by month.
PHP has a few cloud providers. Here’s my one line review of each
PHPFog – control panel looks great, infrastructure is flakey. Support is fast and helpful.
PagodaBox – supposed to integrate with github, but didn’t work for me.
Heroku – this is the original cloud provider and arguably the best. It’s primary focus is ruby but you can run PHP instead, although I haven’t tried it.
Continuous Integration (CI)
CI for me is a must have. AppHarbor has it built-in as part of the deployment process and I’ve also used TeamCity extensively.
For PHP, there are hosted CI providers such as CircleCI (great, but a bit pricey for me), TravisCI (great but open source projects only) and probably others. You could also setup your own TeamCity or Jenkins server (there’s a PHP specific template for it). They all integrate with git/github and can run all your PHP unit tests, coverage etc.
So there you have it, a starter guide for .NET developers looking at PHP. There are still a bunch of features I miss from ASP.NET MVC such as model binding, LINQ etc but for the most part PHP has something which achieve the same thing.
How are you finding the transition from .NET to PHP? Anything which I’ve missed from the above