Tag Archives: Yii

Is PHP still relevant?

Recently I began wondering if PHP was still a relevant language, considering that so much focus is on mobile and that would point towards Java and Objective C.  Also, I’ve recently begun investing a lot of time into learning the Yii 2 PHP framework, which I think is awesome.  Now awesome might not be considered a technical term, but I have to tell you, I don’t mind expressing my passion for something.  I’m a human first, engineer second.

I’m always looking to expand my education and the lack of new books on PHP worried me.  Was I wasting my precious time on a dead language?  It’s only when I looked for material on PHP frameworks, that I found a bunch of titles.  There were certainly a lot of Laravel books.  One thing that impressed me about Laravel is that they really tried to impement the ideas expressed in Robert C. Martin’s book, Clean Code.  Laravel has lots of pretty, semantically pleasing code.  So I played around with Laravel, but I didn’t feel it was quite at the level of Yii, and I just happened to be researching this when Yii 2 Beta came out.  I tried Yii 2 beta and fell in love with it, especially the advanced application template.  It gets you up and running with a mobile first Bootstrap impelmentation, with a working user registration model for both frontend and backend, right out of the box.

Books on Yii 2 are as of this writing, not out yet, but they are coming.  I think they will help usher in a new generation of PHP programmers who will take it to new a new level.  Applications will be built faster, with a more robust and extensible infrastructure.  As of this writing the general release for Yii 2 is about 30 days away.

So there is a lot to be excited about, but getitng back to the original question here, “Is PHP still relevant?”, I can’t help but wonder if it will be overshadowed, either by another language such as Ruby or by a mass migration off the web via mobile.  Of course the Internet wouldn’t disappear, but I could imagine websites as we know them being a relic of the past.

Anyway, I did some searching on Google and found this article on PHP which states that Google has determined that PHP is used on 75% of all websites.  If true, that is certainly a compelling and calming statistic. PHP is widely adopted and used, even if it is not always the flavor of the month.

PHP  does seem to have a lot of haters, and people who are into other languages such as Ruby are not shy about voicing negative opinions about PHP.  A lot of the complaints about PHP seem to be rooted in ideas about what a language should be.  It reminds me of the idealism I found in the Clean Code book.

Now I’m the first to admit, I’m not qualified enough in Java to review that book.  My only exposure to Java is a single book I read on it by Richard M. Reese, when I was recovering from Gall Bladder surgery.  Out with the gall bladder, in with Java.

Anway, I couldn’t help be impressed by the robustness of Java and how its best practices imply a strong architecture that guides a developer into following best practices.  That’s the good news.  On the other hand, it was my impression, from standing at the foot of the mountain, that it would take far more programming hours to do something in Java than it would in PHP.

Also, if PHP programmers tried to follow Clean Code principles too literally, they would bloat their code base with an unmanageable number of files.  I mean unmanageable from a server standpoint, that much PHP would bog down the server. You could have extremely clean code and extremely bad server performance.  Laravel for example, suffers from exactly these kind of problems.

You can solve those problems easily if you have enough money to throw at it.  You can hire teams of Java developers who can build an elegant archictecture that supports maximum code reuse and extensibilty, if I can be so bold as to use that phrase.  Or you could throw server resources at the problems and just feed the beast.  The thing is, doing so will cost you a fortune, and most companies don’t operate with those kinds of budgets.

PHP is a low-cost alternative to that.  So please don’t sneer at it, Mr. Investor, when a start-up tells you they are using PHP. The language, while not as robust as Java, has evolved, and become more object oriented in 5.4 than in previous versions, with namespaces, traits etc.  Frameworks like Yii 2, which use PHP 5.4 and above, can help you do amazing things in a rapid development environment, with templating and code generation.

So here I am making the case for PHP, but I don’t really need to.  The google stats speak for themselves.  Wise, pragmatic programmers already know that PHP is still very relevant.  This doesn’t say anything negative about other languages or frameworks.  They all have their strong points.  Liking one language is not a good reason to hate another.

And finally, looking into the crystal ball for the future, I will address the mobile issue.  I think we will see more cross platform development, like Zend is doing with their Zend Studio product, which I should note, I do not use, so this is not an endorsement.  But I do find interesting that you can build a front-end mobile app and have it be directly connected to your servers and your web architecture.

When I checked it out, I didn’t love the Zend PHP Framework, I didn’t find it intuitive or easy to use, so I’m not really following their IDE.  But I do think we will see more products like this in the future and ultimately, PHP will be supported.  After all, 75% adoption on the web is too much to ignore.

Vote in the poll at the top of the page about whether or not PHP is still relevant and let us know what you think.

Yii 2.0 vs. Laravel

****Update February 8, 2017****

In 2015, with the release of Laravel 5.1, I switched all of my php development onto Laravel. Although I started with Yii2, and wrote the Yii 2 for Beginners book, I have since focused solely on Laravel, and written 3 books on Laravel, the latest being Laravel 5.4 For Beginners.

I outlined the reasons for my switch in the following post.  I had a great learning experience with Yii 2, for which I’m grateful, but I thought it was important  to put this post in context.

I wrote this when I was a complete beginner as a programmer, in 2014.  I’m leaving it up because it is part of my evolution as a writer and programmer.

The original post from 2014:

It was my job a couple of years ago to be a part of the team that decided what php framework my company was going to use.  We did as much research as we could and boiled it down to a choice between Symfony 2 and Yii 1.1.14.  We ultimately decided to go with Yii.  It seemed more agile for our project and we felt the learning curve would be more acceptable.  We did not regret our decision and we have been happy with the results.

Since we made that choice, I have followed the php frameworks to keep up on them.  At the time we made our decision, Laravel was so new, we couldn’t seriously consider it, but it has grown quite a bit since it’s 4.1 release.

In fact it’s grown so much that Laravel has become the most popular php framework.  There are some good reasons for this, including the fact that Taylor Otwell, the author of the framework, developed a very intuitive syntax that is very easy to understand, especially for beginners.

The Laravel framework is built on top of Symfony 2, a popular enterprise framework that is used for developing enterprise-level applications by many developement teams.  Symfony 2 is a robust framework, but I found it to be overly complicated and bloated on resources.  This makes Laravel even more bloated, since it’s layered on top of Symfony.

Although Symfony 2 has beautifully written documentation and an arhcitecture that aspires to be symphonic (pardon the pun), it comes with a steep learning curve, and I didn’t find reliable online tutorials/examples that would show me, for example, how to build a functioning user model.  It was my frustration in trying to learn Symfony that led me to Laravel.  Laravel seems to have more support, more books and a site called Laracasts.com that can get you up and running quickly.

Laravel is written very intuitively, using facades, which are a type of static method, making the code easy to follow.  If I could get inside Taylor Otwell’s head, I could imagine that his framework started as a series of shortcuts for Symfony that made it easier to work with.

Sometimes working with a particular framework is simply a preference for style, since they are all powerful tools in the hands of masterful coders.  I don’t wish to paint any of these frameworks in a negative light.  Programmers, however, constantly have to make decisions and deciding what framework to use is one of the most important decisions to be made.

So now we’re ready to discuss Yii.  I have to say that in comparison to Symfony 2 and Laravel 4.1, Yii 1.1.14 is just plain ugly.  Very powerful and in some ways much easier to use, but not necessarily intuitive or easy to learn.  They do have a very active and helpful community, but their documentation is typically lacking in sample code, a complaint I could make about all frameworks.  Laravel seems to do better on that front, although they are far from complete in that sense.

But Yii is different in more meaningful ways.  Both Symfony 2 and Laravel 4.1 embrace migrations to create database tables, which leads to a piecemeal approach to the data model.  Imagine six different programmers are deciding what tables to create and what attributes (columns) should be in those tables.  Though it’s a popular style of programming these days, it’s not in my opinion a best practice.

Database modeling should be a centralized and well-thought out task.  It should be built before any programming is done, perhaps not entirely, but to the point where the project is well-formed before a single line of code is written.  Relationships, foreign keys, indexes, etc. need to be part of a disciplined architecture, not subject to the whims of individual programmers.

Even in the case where a single programmer is  building the project, they are better off doing the database work with MySql Workbench (assuming MySql is the db) than with migrations.  For one thing, migrations might not enforce constraints on foreign keys correctly and you would not even be aware of the problem.

Ok, I could go on and on about DB modeling, but let’s get back to Yii.  Yii has a browser-based code-generation tool, named Gii, that lets you build your models from the existing database tables.  This allows you to develop the database inside of workbench first, then press a button later to generate the models based on your well-thought out DB design.  Yii also supports migrations if you insist on creating your tables that way.

The code generation doesn’t stop with models, it also allows you to build an entire crud scaffold, with ready-made controllers and views.  This means you can develop a basic application quickly, then focus on the things that make your application unique, the complicated stuff.

While Yii 1.1.14 was powerful, it wasn’t necessarily beautiful.  Now some programmers will simply dismiss this as a cosmetic notion because after all, what does it matter?  Well, the more cryptic the architecture, the slower people learn and the slower projects move, which is never a good thing.  Also, there were some other problems, the most notable of which were the fact that they didn’t use composer for dependency management and didn’t use namespaces.

To their credit, the Yii team, led by founder Qiang Xue, listened to their community and with Yii 2.0 patiently built the most intuitive and full featured php framework I have ever seen.  Imagine out of the box that you have a frontend, backend, a functioning user model with signup and forgot password functionality fully working.  Imagine the framework uses Bootstrap 3 as its default css framwork.  Can you believe you get mobile first design right out of the box?  Well, no need to wonder, just download the advanced template and try it for yourself, you will be amazed at all the features and how fast you will move through it.  Or I should say, just use composer to install it because it now uses composer to manage its dependencies and composer is really easy to use.

It’s hard to describe the beauty of the architecture, you just have to see it for yourself, but it is much more concise and intuitive than it was before.  As of this post, Yii 2.0 is still in alpha, but the beta is coming quick.  Not a lot of docs yet, but you can find what they have here:  http://stuff.cebe.cc/yii2docs/guide-index.html.  My bet is that you will be hearing a lot about Yii in the next year or so as it releases into production.

If you have a preference for one of the three frameworks in this post, please take our poll and feel free to sound off in the comments.