Yii 1 to Yii 2 differences and enhancements part 1

A number of months ago I made the switch to the Yii 2 framework from the Yii 1 framework. My intent in this post is to outline some of the differences and enhancements I have found from switching to the latest Yii framework.

A framework is a set of code written to help in common programming tasks for example handling data, creating forms and data tables etc.

When I first started programming I did not use a framework as I did not understand the benefits properly of using a framework. The above tasks would have been written from scratch and would have essentially duplicated code that was already out there for solving these common tasks.

When I first started using a framework it made my life much easier and the end product was of a better quality to time ratio than not using a framework. The same quality could be achieved without the use of a framework but would take longer and the programmer would be essentially writing code for common problems that already have a solution.

I would also like to mention that frameworks provide a consistent structure to the code that a programmer writes so that one way of solving a problem at one end of the code would be the same way the problem was solved at another end of the code. This makes it easier for other programmers to understand the code base.

After using the Yii 2 framework here are some of the differences and enhancements that I have found …

User Identity

In Yii 1 a “User identity” is a class that extends “UserIdentity” and handles the authentication and identity of the logged in user. I would create a class that extends “UserIdentity” create some predefined methods and let Yii handle the authentication.

In Yii 2 a similar approach is used except I do not create an extended “UserIdentity” class but instead I “implement” “IdentityInterface” in my User class. In Yii 1 I would have a “UserIdentity” class and a “User” class, in Yii 2 I just have the “User” class. Similar to Yii 1 I create some predefined methods in my “User” class and let Yii handle the authentication.

Active Record

Active Record has some key differences going from the Yii 1 framework to the Yii 2 framework. In Yii 1 Active Record was used like so …

Or …

Or …

In Yii 2 Active Record is used as follows …

Or …

Or …

Basically in Yii 1 there where a lot of methods that where created for specific purposes “findAll”, “findByPK”, “findByAttributes”, “findAllByAttributes”, “deleteByPK”, “deleteAllByAttributes” etc.

Yii 2 can do all of the above purposes but the syntax is much more flexible in that it does not have methods for each purpose but flexible syntax that can be used for flexible purposes.

Also note how in Yii 1 I used array() and in Yii 2 I used []. That is because at the time of Yii 1 array() was the PHP syntax used for creating arrays and at the time of Yii 2 [] could also be used to create arrays. Not really a Yii issue but still the array syntax looks better in the more modern version of PHP.

Use

Due to the Yii 2 framework being written at a time when the version of PHP was later and more modern the Yii 2 framework has made extensive use of the “Use” statement and “Namespaces”. Yii 1 did not make use of these at all probably because they where not implemented in PHP at the time the Yii 1 framework was written.

Nevertheless I feel the “Use” statement and “Namespaces” give the code a more professional feel and it means that the code is only made use of when the code is needed.

I will be updating the blog with more articles on Yii 1 to Yii 2 differences and enhancements as I go a long. I already have plenty of differences lined up but I will be saving them for the next article in this series.

PHP exceptions, try, catch not working

I was porting over some code a few days ago from a standard standalone PHP script I had written some time ago. I was wanting to use the code inside a larger new web application I was building for personal use.

I was absolutely certain I had copied all of the code line by line into the new program and refactoring it as I went along making sure I did not do anything to break anything.

Everything seemed perfect until I came to run the code and was left baffled by “Uncaught exception” PHP error messages. These error messages did not occur all of the time but only some of the time which made debugging worse.

However due to the nature of the functionality of the code was to be expected as their were aspects of the code that were dependent on an outside source and could be variable.

So I looked through my code and all of the try / catch statements were there and the exceptions as far as the code was concerned should have been handled correctly. I had reached a block that I could not get past, at least for a couple of hours.

After doing some research on Google the answer came to me. I had written my try / catch statements as follows …

The problem was due to name-spacing of the application I was porting the code into. The old standalone PHP script did not use any name-spacing so this was never a problem. The new PHP application did use name-spacing so the try / catch code needed to be written like so …

Notice the “\” symbol. That would be telling the Exception to escape to the global space. After adding these corrections to the code it worked perfectly.

Front-end vs back-end development

Is there such a thing as front-end vs back-end development in today’s web development world? How much do these cross over and even into other roles entirely? What is front-end developer or a back-end developer expected to know or do?

Well there is no easy answer to all of these questions, with today’s expectations both front-end and back-end developers have a lot expected of them. In very basic terms we could say the roles are briefly defined as follows …

Front-end developer

Expected to know HTML, CSS and be good at graphic design.

Back-end developer

Expected to know how to create databases and PHP.

Ok so that is defined very basically, now I will take into consideration all of the criteria not mentioned for each of these roles below …

Front-end developer

Based on the above we could also add the following as potential criteria to the Front-end developer role …

  1. Expected to know CMS systems such as WordPress.
  2. Expected to know front-end frameworks such as Twitter Bootstrap for example.
  3. Expected to know Javascript.
  4. Expected to know any number of Javscript frameworks such as JQuery, KnockoutJS etc.
  5. Expected to know any number of CSS frameworks that are available such as Less, SASS etc.
  6. As mentioned earlier, expected to know graphic design even though graphic design is a completely different discipline altogether.
  7. Expected to know server side templating languages, for example Smarty.
  8. May be expected to know a bit about server side languages so they can incorporate the HTML templates into the web app.
  9. Expected to know about SEO so the website can be coded to accommodate the SEO.

Back-end developer

Based on the above we could also add the following as potential criteria to the Back-end developer role, the first 6 bullet points are the same as the front-end developer role but are still valid, this is the cross over between roles I was talking about and how it blurs the lines between front-end and back-end development …

  1. Expected to know CMS systems such as WordPress.
  2. Expected to know front-end frameworks such as Twitter Bootstrap for example.
  3. Expected to know Javascript.
  4. Expected to know any number of Javscript frameworks such as JQuery, KnockoutJS etc.
  5. Expected to know any number of CSS frameworks that are available such as Less, SASS etc.
  6. Expected to know server side templating languages, for example Smarty.
  7. Expected to know HTML and CSS.
  8. Expected to know any number of backend frameworks, for PHP this would be frameworks like Laravel, Yii etc.
  9. Expected to know any number of database environments and their associated query languages for example MySQL / SQL, MongoDB / BSON etc.
  10. Expected to know any number of code libraries.
  11. Expected to know how to use version control systems such as Git, BitBucket etc.
  12. Expected to know any number of backend languages including all their frameworks and libraries. For example back-end developer could know C# but also PHP.
  13. Expected to know how to create and manage web servers.

So there is probably a lot more that I have not thought about or mentioned but that just about sums it up for now. You can see there is a big cross over between the skills especially in the area of Javascript. Javascript as a language is nothing like coding HTML but Front-end developers can still be expected to know Javascript.

Back-end developers can still be expected to use HTML even though they are Back-end developers. You also have to appreciate that most of these skills are multi-disciplinary and each skill in its own right could take a vast amount of hours to learn and maintain properly.

For example Javascript developer should be a role in its own right. Graphic designer should be a role in its own right. Web server administrator should be a role in its own right. Learning a single web framework like Laravel should be a role in its own right. Not one developer learning multiple frameworks.

The way the skills are listed above in the bullet points make them sound easy and degrades the time and effort it takes to learn the skills properly. However employers list the skills in the same manner as above in job descriptions they send out to potential candidates.

No one could learn all of those skills and be proficient in all of them. The time it takes to maintain such skills would be too much for one person to accomplish if taken to the degree of mastery.

Maybe some employers know this and do not expect the developer to be a master at all of them. Maybe some employers just copy other employers job descriptions without knowing that the other employer did not expect the developer to be a master at all of them.

Any employers listing too many skills that a developer should be a master at should be looked upon as not being very grounded in reality and taken with a pinch of salt. It is not just the time to learn the skill but to maintain them as well.

These technologies are constantly evolving and there is a lot of maintenance time involved as well. To be a master the skill has to be maintained and as mentioned earlier there is no way in reality that one person could be a master at all of them.