Yii 2 For Beginners

I’ve been studying the PHP framework scene for the last 3 years in depth. I’ve done comparisons on all the major frameworks, including Laravel, Symfony, and Yii, and in the end I chose Yii 2 as my framework of choice. You can see a little of the history of this, my blog, Yii 2 vs. Laravel, talks about discovering Yii 2 when it was only in Alpha. After trying all of the other frameworks, watching countless videos and reading many books, I settled on the one PHP framework that had no book whatsoever to help beginning programmers.

I always felt, if only there were a book for beginners, I could learn this quickly, I just needed someone to point the way. Instead, I learned piecemeal by gleaning tips from the forum, the guide, and from the fact that the framework was very intuitive. Even though I made progress, I still found myself wishing for the book. Learning this would be so much easier, if I just had a book!

How ironic is it that 6 months later, I end up writing the book I yearned for? Well, I kept very detailed notes on my development and I noticed over time that I could organize them into a book. I just had to be willing to sacrifice the time, which could have been spent developing actual sites, which instead had to go into writing lessons for other people.

It was a tough choice, but I chose to write the book, it seemed like a worthwhile endeavor. It turned out to be an incredible challenge. It pushed me mentally and physically. I spent long nights picking apart every detail, striving for excellence and clarity, so that others could more easily understand how to use Yii 2.

I started out fairly objective about PHP frameworks, simply looking for the easiest framework that could support robust development. Although I had a lot of respect for Symfony, I felt it was overly complicated and bloated. I didn’t like having to learn Yaml, even though it’s not difficult. I didn’t like having to write every route through the site. I didn’t like what I saw of doctrine. And I didn’t like the fact that alhough they had beautifully written documentation, there was no real effort made to service beginning programmers. This has a downside for businesses that have to hire programmers because it then becomes more expensive to train programmers. So in that sense, Symfony is an expensive discipline.

Laravel, on the other hand, went out of it’s way to service beginners and I loved that about it. The problem was, I just never felt like they were helping me build anything. None of the examples I found at the time, for example, dealt with a robust user login and verification. And without that, what kind of site do you have? And then, because it’s built on top of Symfony, it brings in Symfony’s bloat and performance issues. The other programmers at my company didn’t like it either. It didn’t seem robust enough to them, though in fairness, I should say that it was very new when we were doing those comparisons.

I turned to Yii 1.1.14 because my company had settled on it and the programmers loved it, but I found it difficult to grasp personally. In retrospect now, since I came up to speed on Yii 2, I understand 1.1.14 a lot better, but at the time, it seemed so difficult to use. The reality was that it was difficult to learn, but easy to use. You just had to be at a certain level as a programmer to get that. I hated that about it.

Then I discovered Yii 2 and everything changed. Whereaas Yii 1.1.14 was old and ugly, using old array syntax, no namespaces, etc., Yii 2 was a beacon of modern PHP. And being beautiful architecturally in this case is not just a superficial thing, it is extremely versatile and useful, way more intuitive than it’s predecessor. I have so much enthusiasm and passion for it, that I want to share it, and this was one of the motivations to write Yii 2 For Beginners.

Also, I know that right now, a beginning programmer is wandering around, wondering how to go from novice PHP programmer to framework developer. That’s a tough transition to make. I know just how frustrating it is. I want to help.

There are many, many, more advanced, and more qualified programmers than me, who apparently don’t have time or the interest to bring it down to the beginning level, so that task has fallen to me, at least in the short-term. I didn’t plan for this to happen, I’ve just found myself in these circumstances. There is no other beginning Yii 2 book out right now. So I did the best I could and I put a lot of effort into making Yii 2 as understandable for a beginning programmer as possible.

I realize that by being so into Yii 2, I no longer have objectivity to the other frameworks. That was bound to happen. I had to pick one. And when I get into something, I go all in.

So Yii 2 For Beginners is out now. I’m really glad I could contribute something to help beginners harness the potential of Yii 2, it really is an amazing framework.

Now that the book is done, I’m looking forward to getting back to application development, I hope to have a new site done in January. After that, I’m going on vacation, if I’m still breathing…

I will continue to support PHP and Yii 2 beginners on this blog, and hopefully continue with the polls, which people seem to find interesting. Over 1000 developers have voted in my polls. I hope to continue to develop this blog as a resource for the community.

Yii 2 PSR Coding Standards

I’d like to take a moment and welcome everyone who is finding their way to this blog. We have readers from all over the world. A lot of people have voted in the polls and the most active one is on my Yii 2 vs. Laravel post. Over 700 people have voted in that poll, so thank you to everyone who has participated.

Recently I read the book Clean Code by Robert Martin and it got me thinking about coding style and standards. I saw on the news release for Yii 2 that it follows the PSR standards, however I found some instances in using Yii 2, where it’s not following the standard.

For example, the naming of a private variable in the LoginForm model:

class LoginForm extends Model

public $username;
public $password;
public $rememberMe = true;

private $_user = false;

Accodring to the PSR-2 standard:

Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility.

Personally, I like the way Yii 2 is doing it there, it seems very intuitive to me and I couldn’t understand why the PSR standard would be against it. So I posted a question about in the Yii 2 forum and got an excellent response from forum member DocSolver. This is what he said:

“An argument in favour of the PSR-2 standard: if you change the status of a property from protected to public, you have to refactor all references to it.

The underscore in the name reminds me of to the old programmer’s habit of prefixing a variable’s type in its name (e.g. “floatAmount” and “strCustomerName”), which was popular in older languages when IDE’s were nonexistent. But now these habits are considered a bad practice by authorities such as Robert C Martin (see book “Clean Code”).

Because of this, I would say that your IDE should notify you about a variable being protected, instead of a naming convention that will probably be violated by many people. So I would vote in favour of conforming the Yii2 codebase to the PSR-2 way.”

DocSolver made some excellent points. But one thing to keep in mind is that the Clean Code book covers Java, which is a strongly typed language anyway, meaning that the variable type is explicitly stated with descriptors like string and int. So java gives you more hints going in.

Personally, I like the underscore because it pops out at you like a speed-bump. For example, in the code snippet above, it helps me understand that $_user is not from the form input, it really jumps out at you as different.

Anyway, this might be microscopic overkill on style, but I do feel standards are important and should be respected. One of the main points in the Martin book is that programmers spend much more time reading code than they actually spend coding. It’s so true. And that means things like this become important because it’s likely someone will have to read your code and the easier it is for them to understand it, the better.

I wrestled with this issue a bit because I would be much more comfortable using the underscore for private variables, just seems to have stuck in my head as a good way to go, but I’m going to sacrifice that peace of mind I get doing it that way to stick to the standard. I think following the standard is actually more important, at least in this case. Plus DocSolver made a pretty convincing case of why following the standard is actually better.

Internally, we can resist change, but that is just a form of inertia. I remember friends being resistant to the new array notation because they were so used to the old style, which to me looks like dinosaur code. I don’t want to be in the dinosaur camp.

I’m not a fan of mindlessly following what we think others expect from us. I think the controversy on TDD is an example of that. On the other hand, when programmers come together to set a standard, it’s worth taking it seriously.

In terms of Yii 2 projects, I will not be refactoring the framework code just for the sake of eliminating an underscore, that would silly. Plus, I would probably just mess it up anyway. I’ll just be mindful for my future code, when creating a private property, to keep it clean of the identifying mark. Let us know what you think. Take the poll and let us know if you follow the PSR-2 Standard.

Yii 2 extensions

I was lucky enough when I started playing with Yii 2, about 6 months ago, during the alpha release, to get some help answering the most basic questions about creating relationships and tying models together. That help came in the form of Kartik Visweswaran, a Yii 2 innovator and enthusiast locaed in Bangalore India. Not only did Kartik help out in the forum, taking the time to support users, he also provided tutorials on the wiki, including this tutorial for a sortable, eager-loading search result in GridView.

And Kartik also built incredible extensions for Yii 2, so many in fact, he has them listed on his own site at Krajee.com.

I knew I was going to love his work when I implemented the tutorial I mentioned above, scenario 2, which was right on the money and took less than 15 minutes. I also integrated Kartik’s social extension, yii2-Social, which got me into Facebook widgets, again with minimal time spent. Part of the reason it goes so smoothly with integrating his work is that he documents it so well.

Last time I counted, he had 28 extensions/goodies on his site, with an incredible selection extending Gridview, navigation, form builders, file management, helpers, quality Bootstrap stuff to make your app pretty, and much more. If you have not checked it out yet, you are really missing out.

How lucky are we to have a great framework to play with, but great contributors too. This is what makes programming a lot of fun.

You can find a lot of cool extensions also at https://packagist.org/. Just type in Yii 2 and you will see the list of available items, 350 of them at this writing.

Now with Composer fully utilized by Yii 2, installing an extension is very simple. Just add it to your composer.json and do composer update and one minute later, it’s installed. Depending on the extension, you might have do some config, but that seems to clearer and easier these days with Yii 2. Kartik documents his extensions extremely well, so you will be up and running quickly.

If I understand Kartik’s blog correctly, he works full time programming and does his Yii 2 extensions in his spare time. You know you love something when it is your work and your hobby too. It helps explain why he is so prolific and so good at what he does. Also check out his WebTips blog, there is a great tutorial on the Yii 2 FileInput widget.

A lot of programmers are humble by nature and they don’t get the credit they deserve. This certainly applies to the core Yii 2 team, and also to the supporting developers like Kartik. These guys do an amazing job. I’m not shy about pointing that out. A lot of hard work goes into creating this stuff, and as a beneficiary of that work, I really appreciate it.

Another way to say thanks to developers like Kartik is to click and visit the sponsors on his site, and of course there is the donate button. A little contribution makes a big difference to the quality and quantity of open source options. If you find yourself using contributions often, please give something back. These guys will really appreciate it and you will see more great innovation that will help your development needs.

One of my goals with this blog is point people in the right direction when it comes to Yii 2, at least as best I can. So I’m definitly pointing in the direction of Kartik for Yii 2 extensions.

In the future, I’d like to profile more developers, especially if they are doing interesting things with Yii 2. Now that Yii 2 is in General Release, we will probalby see even more contributions. It’s a very exciting time for people who love the Yii 2 framework. Feel free to comment and take the poll, and please contact me if you wish to contribute to this blog.

Yii 2.0 Officially Released

The stable version of Yii 2 is officially released today and I thought I would mark the occasion by writing about it. I think the release of the framework is significant and it will impact PHP programmers for many years to come.

You can read the Yii 2 official release here.

The more I learn about Yii 2, the more I like it. Even though it was only in alpha when I started learning it, and the documentation was incomplete, I was able to go much further on Yii 2 than other PHP frameworks like Symfony and Laravel. Not only could I see obvious benefits in the architecture of Yii 2, but Yii 2’s advanced template provided me with enough structure out of the box to get something started, and that something included mobile-first bootstrap design with a working user model.

Sometimes, all we need is to be able to follow a well-thought out implementation and the advanced template is certainly that. With just a little modification, I was able to create a custom, and I should say simple, access control for admin, roles, user types, etc. I was able to do it quickly, getting an application going in about week, which considering where I was as a programmer when I started, was pretty cool.

Yii 2 is just incredibly well-thought out. It seems like the dev team has anticipated just about everything you could ever think of and found a way to support it. The behaviors and event architecture is feature-rich and very intuitive. And then of course there is Gii, the amazing tool that cranks out code for you and saves you even more hours of work.

I think the biggest frustration anyone feels learning a framework is dealing with the sheer size of it. This is true of any of the big frameworks and also true of Yii 2. You can’t just snap your fingers and master it.

Yii 2 is like a Ferrari. You want to jump in and go and leave tire tracks, but first you have to take the time to learn how to drive. What a drag.

Maybe that’s not the best metaphor because it should be understood that it takes significant time reach a level with PHP that allows you to run at high-performance. Gone are the days when PHP was just a quickie scripting language that let you build a form for your webpage. What we have now is more like rocket science, or, and not to be comical, more and more like Java.

Anyway, Yii 2 has a huge code base. When you work your way in recursively, deep into the framework, you find yourself in strange territory quickly. And I guess that’s what makes the surface so amazing. The deep wizardry is extracted to base classes that allow us to have these nice intuitive methods to work with.

I’ve documented hundreds of pages of work in Yii 2, so much so, I may even write a book for beginners on it. If you really want to learn something, try writing a book about it. It’s a very exhausting, yet effective technique.

And yet, for all that work, meticulously documenting controller methods, models, view architecture, and form models, etc., I still find it hard to explain exactly which features make me like Yii 2 so much. And I think the reason for this is a non-programming term: flow. Yii 2 just seems to flow.

I tend to think in creative terms. If I have an idea, I want it to flow. I don’t want the idea to come up against endless barriers because that is what kills ideas. The extent to which Yii 2 helps my creativity flow is a measure of how much I love it. And I love it a lot. It’s driving me to learn this alien rocket science, so I can take the new rocket ship up and see what the world looks like from that orbit.

A new generation of programmers will do the same. They’ll take it to heights none of us have ever seen before. PHP is in fact alive and well and flourishing in Yii 2. Check out the poll results in some of my other blog entries and you’ll see what I mean. I find this all very exciting.

Anyway, I’ll conclude by congratulating Qiang Xue, Samdark, Cebe, and the other dev team members for reaching such a great milestone. They made something really special. If I could say I only know one thing about programming, it would be this: it ain’t easy. So, great job guys and congrats again.

Bitcoin Goes to Zero!

I’m not a fan of Bitcoin, I think it’s nothing more than a virus masquerading as a fake money-making opportunity. I have felt this way for quite some time, and now that the price is dropping, others may be finally seeing it for what it is: a scam.

To understand Bitcoin better, I downloaded a Bitcoin wallet and looked at the mining process. So without getting too technical, Bitcoin is written in Java. You download a java program called a wallet to your computer and it connects you to other computers to allow you to trade in Bitcoins. If you engage in Bitcoin’s mining process, in which you trade computer resources for free Bitcoins, you can earn money, suppossedly. The problem with mining is that it costs more to extract the coin in terms of electricity and computer processing, than you make in coins.

The actual coins are data stored in the wallets. They can be trasferred from one machine to another as long as they obey the protocol. The interesting part is that there is no central database of coins or coinholders. Because the currency is a digital file that transfers from machine to machine, it is completely unregulated and uncontrolled. It operates completely anonymously.

So if it’s just a piece of data, what gives a Bitcoin it’s value? The short answer to that is: idiots. The ones who think it represents more than what it is, which is nothing more than a data transfer protocol. There is no inherent value in it, only that which like-minded people agree upon, and more often than not, like-minded people who are completely ignorant of what it really is.

I know that sounds kind of harsh, but I feel I need to be. Too many people are going to lose big money over Bitcoin. It caught on like Beatlemania and people jumped in with no idea of what they were really doing, just happy to be a part of it, often even confusing the ownership of the coins with ownership in the company, since it traded electronically like a stock. It was the nature of the electronic transfer and ease of trading that let it grow like wildfire, along with the idea that you were getting in on something.

One group of people were a little bit wiser about Bitcoin. These are the money-launderers and criminals who needed a way to move money around with the government knowing about it. Which government? Any government. In other words, no goverment control whatsoever. That means no taxes, no regulation, no safety net.

I’m not a big fan of taxes, they corrode everything they touch, but a system outside of government answers to no one, and when it comes to your money, this is a really bad idea.

All currencies are based on something, usually it’s the idea that the issuing government is able to manage the currency by collecting taxes and regulating the money supply. In reality, this is a pretty bad system too, but at least it is based on something. Bitcoin has air behind it. It is the all-time greatest hack/virus the world has ever seen.

The founder created a protocol, gave himself a million coins, then patiently waited as the foolish and suseptible drove the value up to $600 per coin. So he got $600 million in value at its peak, with no obligation whatsoever to the criminal underpinnings of the transactions, no responsibility to the drug money, the gun money, and who knows what else got moved around because of Bitcoin. He gets to walk away clean. My guess is that when Bitcoin hits zero, he will disappear quietly, with his millions hidden away in foreign bank accounts.

Bitcoin will hit zero, make no mistake about it. The currency has nothing behind it except the confidence of the limited number of people who trade in it. Now we are seeing that confidence shaken. When it hits a tipping point, the bottom will drop out. I think it’s coming sooner rather than later.

Is TTD Dead? Take the Poll

So I started wondering if Test-Driven Development was a worthwhile practice, since our company had not adopted it yet, but we were thinking about doing so in the future. I started searching in¬†google, when I came across the “is TTD dead?” video and decided to watch it. It’s fascinating stuff.

The creator of Ruby on Rails, DAVID HEINEMEIER HANSSON, wrote a blog entry “TDD is Dead, Long Live Testing.” ¬†This blog caused controversy and the people on opposite sides of the discussion, the originator of TDD, Kent Beck and noted author Martin Fowler, and David Henemeier Hansson, came together to talk about it. If you haven’t checked out the video, I highly recommend it.

Even though I don’t have a lot of personal experience with TDD, I really connected with David’s points. Programmers often take what starts out as a good practice and turn it into self-serving religious dogma, a point David makes in his article. It’s so true. In our rush to adopt best practices, we end up with a rigidity that doesn’t allow for asking the pertinent questions.

I’ve always been a loose cannon, loyal to what I think is the truth, as oppossed to what I think is expected of me. I felt that TDD added an unnecessary layer of complexity, especially when you are dealing with frameworks like Yii 2. The framework carries so much of the burden, that designing tests that take into account the full framework becomes cumbersome. So the workaround are mocks, which as David pointed out, are likely to cause problems and really fail to achieve the promise of a fully tested implementaiton that can be tested in seconds.

David doesn’t suggest abandoning testing, just the TDD method of writing the test first. I really think he’s on to something. I’ve never been a fan of what I can best describe as a speedbump to creativity. Writing the test first strikes me as a speed bump. You can force yourself to slow down for the bump, but it really impacts momentum.

The funny thing is though, is that I did a quick meeting with our 4 progrmamers, and each one of them expressed interest in us adopting TDD, the exact opposite of what I expected. Most felt it would save time over the life of a project. Codeception was brought up and that is integrated into Yii 2, our framework of choice.

I remember the old television commercials where 3 out 4 dentists recommend something, and you certainly did it, otherwise you could expect one heck of a toothache. Here I have 4 out 4 programmers in agreement, fairly compelling. So we decided to review our options and revist before we start our next project. I have a feeling we will adopt a solution that incorporates Codeception, even if we don’t write the tests first.

My intuition also tells me that an influential person like David Heinemeir Hansson coming out against TDD probably marks the end of that trend. It will be interesting to see where this discussion goes. In the meantime, take the poll at the top and let us know what you think of the future of TDD. Thanks!