Is Zephir worth learning?

Not so short answer: If you want to learn something that makes you more marketable, then no. It is better to learn C++ instead. If you just want to play with it, maybe write an extension or two for your project, then Zephir is fun language to learn.

I checked for jobs dice.com and other popular job sites and I found 10 jobs on all of them combined. This is 10 jobs for both Zephir and Phalcon combined (searched separately). There are no popular websites I know of, that use Phalcon. There are some Russian and Polish websites that use it and I don’t know how popular they are but none that I have seen in the US.

Phalcon is the fastest framework but it hasn’t been widely adopted. In fact, I don’t know of any site, or web app that uses it. For those that don’t know: Phalcon (a PHP framework installed as any other PHP extension) is written in Zephir.

At first, I thought the lack of adoption of Phalcon (or Zephir) was because it lacks a wide variety of features (data mapper pattern being one of them). While that is true, that is not the only reason for it. Most websites (or web applications) run on shared hosting. They don’t have the ability to install custom extensions. So, unless the web hosting provider already has Phalcon installed, you are out of luck. This reduces Phalcon’s demographic by a significant percentage.

I think Zephir’s usage is directly proportional to Phalcon’s usage. As far as I can tell, it doesn’t seem very promising. One day when PHP let’s you install extensions like we install libraries through composer, it will have a better chance. For the near future, however, there are few to no job prospects for it.

Zephir (like its makers point out) is not a general purpose programming language. It was made for a very specific purpose of developing Phalcon and overcoming the adoption barrier. It is a bit limiting and expressing complex problems in it can be difficult. However, if you can get the hang of it, I think it is worth checking out.

On a side note: there is an app that converts your code to C++, making it compilable. It is called Swig and can be found here: http://www.swig.org”

A second look at Zephir

Ever since I found out about Zephir, I have been very curious about it. I mean compiled PHP code. How awesome is that? Also, Zephir code doesn’t have to be compiled into a PHP extension. You can just convert it to binary and run it normally like any other PHP code.

Why spend a lot on Zend Guard or any other encoding software when you can do it for free with Zephir? There is a caveat though: Zephir is not PHP. It has similarities but it is not completely PHP code. So, you would have to learn whqt works and what doesn’t.

One thing to note is that it is still in beta. Phalcon has been out for a while so I figured it would at least be 1.0.0 stable. However, that is not the case. At the time of writing this article, the latest version is 0.9.8b. I have always wanted to write code as PHP extension so it would run much faster than regular PHP code.

I read (haven’t benchmarked yet) that not all compiled Zephir code runs faster than regular PHP code. This is probably because of some I/O bottlenecks. If you are planning on using Zephir, make sure you benchmark the performance for your use case. If compiling doesn’t make things any faster then its not worth the extra pain to compile and install a PHP extension.

Some very cool features in PHP7

I played around with PHP7 over the weekend and I have to admit it has some very cool things I have wanted PHP to have for a while, and then some. Here are some of them (in no particular order):

  • Performance

    PHP7 will increase the performance of PHP scripts by 50-60%. Nuff said.

  • Anonymous Classes

    I have always been able to do this in Java where I can just define a required object and pass it along.
    Now, you can do the same in PHP (I know that it is a double-edged sword and can leave your code un-organized
    if not used wisely). Here is an example:

    This can be pretty useful if all you are troubleshooting a production-only issue. Ideally, your testing
    environment will be exactly like your production environment. And, you are able to re-produce every production
    bug in QA but real world is rarely meets ideal standards. We, now, have anonymous classes to help.

  • Scalar type-hinting and Return type hinting

    Ever since PHP introduced type-hinting, I have been using it everywhere I can. It helps with static analysis of code
    and helps prevent bugs (although I love the freedom that comes with PHP not being a statically typed language).

    Note: like previous versions of PHP, PHP7 will not force its user (the PHP developer) to type-hint anything. So, old scripts
    will still work.

  • Removal of date.timezone warning

    Every time you use a date method, PHP would throw a warning if you didn’t have a default timezone set in php.ini
    or in your code (using date_timezone_default_set()). PHP would default to UTC and throw the warning. This warning
    has been removed in PHP7. Good riddance.

  • Null Coalesce Operator

    Also called the isset ternary operator, gives you a more convenient way of … doing this:

There are more cool and useful features I have not mentioned here. You can read about them here: https://wiki.php.net/rfc

PHPSpec: Mocking methods of the object being tested

PHPSpec is very opinionated and won’t let you mock anything of an object being tested. It helps you write better code. This, however, causes issues when you are writing specs for legacy code which (usually) was not well designed.

There is a way you can get around this limitation – a child class. Let’s say the class you want to test is called “Car” and it has a method called “getNumberOfDoors” that you need to mock. Just write a child class and mock the method in it. Here is the code:

You can make PHP extensions in a PHP like language

I recently came across an article which spoke about a language called Zephir. It was written with the goal of letting PHP programmers write PHP extensions. I was really happy to read this. For quite some time I have dreamed of creating PHP extensions but the fact that I would have to learn C kept me away from it (although I would have eventually done it). And, I have been jealous of Lisp which can be extended using Lisp itself (unlike PHP which requires you to know C to extend it)

Zephir looks almost like PHP. There are a few differences but if you are a PHP developer it will probably take you mere hours to get the hang of it. The same guys who wrote the Phalcon framework have written Zephir. For those of you who don’t know what Phalcon is, it is a framework written as an extension of PHP. It is the only framework of its kind (as far as I know). All other frameworks are written in PHP.

Imagine the speed boost something like this can give your application. Binary code executes much faster than interpreted code. You can shift all your intense computing code to an extension and improve the performance of your application by quite a bit.

You can learn more about Zephir here: http://zephir-lang.com/tutorial.html

Selenium Chrome driver nuances

When writing test, you generally tend to run the test in one browser and expect it to work on all the rest (except maybe Internet Explorer). I had ran my tests on Firefox and they succeeded but not on Chrome.

In Firefox, if you click on an element that is not currently visible on the screen, the browser will click allow it and not cause any issues. However, Chrome will complain saying that the click will be caught by another element (depending on how you structured your HTML). So, you have to move to the element before you can click on it.

Testing multiple browsers with Selenium2 in PHPUnit

You used to be able to run test on multiple browsers with PHPUnit Selenium test class by specifying a $browsers property. You can still do the same with PHPUnit’s Selenium 2 class made for web driver. Here is how you do it:

Fastest way to traverse an associative array

PHP has array_walk() which can be used to traverse an array and modify it in place. I wanted to find out if array_walk() would be faster than a loop written in PHP (for, foreach). I mean, PHP’s code is written in C/C++ and traversing anything has to be faster in C than in PHP.

So, I wrote a script to traverse an array of about 100,000 elements. Below are the results (formatted for better readability):

foreach traverses an associative array faster than array_walk() and for loop. In fact, it was much faster than both. I had expected array_walk to be faster but I was wrong. You will notice that the while loop isnt in there. I figured that a while loop would take about the same time as a for loop. Here is the script I ran for my experiment:

Convert an array to XML in PHP

I haven’t benchmarked the memory consumption of this method versus just traversing the array yourself and writing XML but I can’t see anything else being more efficient than this.

Code Assist in Zend Studio (eclipse based)

Have you noticed there is a delay autocomplete suggestions when you type in a variable name or a function name? My typing speed is decent and I found the code assist delay to be too much. By the time Zend Studio was ready to show me the suggestions, I had already typed the variable name.

By default, Zend Studio has a 200 millisecond delay in showing code assist. You can shorten it if you feel the delay is too long. I set it to 100 milliseconds.

Go to Preferences -> Java -> Editor -> Content Assist -> Auto-Activation and decrease Auto activation delay from 200 to 100 (or whatever suits you better).