Claudiu Persoiu

Blog-ul lui Claudiu Persoiu

How to make use of the Xiaomi Air Conditioning Companion in Home Assistant in only 20 easy steps!

without comments

Xiaomi Air Conditioning Companion si Home assistant

Step 1:

Buy a Xiaomi Air Conditioning Companion without first researching how well it is supported by Home Assistant.

Step 2:

Realize that the Chinese power socket for 16A is different from the 10A one.

Step 3:

Realize that nobody is selling a 16A socket adapter for a 10A China power outlet in Romania.

Step 4:

Order a 16A power socket from China.

Step 5:

Wait 2 months for both the socket and the device to arrive from China.

Step 6:

Realize that there is no adapter for the wall outlet from China also.

Step 7:

Find the only seller that will sell a modular outlet that matches outlet box, probably by mistake

Step 8:

Wait for them to tell you that it will be delivered in a month

Step 9:

If it did not arrive yet, wait for a socket module to be delivered

Step 10:

Install the wall module and power socket

Step 11:

Connect the Xiaomi Air Conditioning Companion and AC for the first time

Step 12:

Realize that the Xiaomi Mi Home App has been updated in the meantime and there is no working tutorial on how to get the password for Home Assistant.

Step 13:

Figure out (after many tries) how to get the gateway password.

Step 14:

Add it to Home Assistant and realize that the gateway is not well supported and that the only things you can do from Home Assistant are sound the alarm and change its volume.

Step 15:

Find the xiaomi_airconditioningcompanion module and realize that you didn’t need the gateway password in the first place.

Step 16:

Downgrade the Xiaomi Mi Home App to get the token as specified in the instructions:

Step 17:

Realize that you don’t have the right version of to use the module.

Step 18:

Move to Hassbian from

Step 19:

Finally, install the extension and setup the module.

Step 20:

Now the only thing that remains is to enjoy the comfort of controlling your air conditioning from a few feet away, without ever using the remote!

Written by Claudiu Persoiu

13 October 2019 at 7:47 PM

Posted in Diverse

Tagged with

How I use Magento2 on my local with Docker and Docker Compose

without comments


Please note that this solution is tailored specifically for my needs and, while your needs may vary, don’t worry, everything is on GitHub, so feel free to take what you need.

I would like to add that this is not a “you’ve been using Docker with Magento2 wrong, this is how it’s done” kind of blog, I just want to share what I’m using and how. It may not be the best fit for you, but maybe you will find something useful.


For almost 2 years I’ve been using Magento2 in Docker containers. I’ve been doing that before, but I must admit that it was because I had to, not because I’ve seen the light, I mean advantages.

As you may know Magento2 is not exactly a small and light app, it’s quite heavy on the resources, especially during development.

Compared to a VM, with Docker you get:

  • Speed: I think the speed is one of the biggest advantages, you can stop and start containers very fast, only the first build will take time, after that it will be very fast;
  • Light on resources: Compared to a VM, the container does not need to include the entire operating system, so it will not take a lot of space on disk and will not use a lot of processing power, because it’s not an entire OS doing… well… OS stuff, it’s just a server most of the time.

What you don’t get:

  • Learning curve: if you don’t know Docker and Docker Compose, it will be less intuitive at first;
  • First setup: harder to setup at first, if you have been using a VM for a long time, you will feel that you are going against the tide, but I assure you, in the long term it will be a lot simpler this way.

Taking the above into consideration, I would like to say that when I’ve started with this setup I was using Linux with 8G of RAM. One of my colleagues even wished me good luck on installing Magento2 on a ultraportable 8Gb RAM system. He wasn’t even sarcastic, more like pitying me for my bad workstation selection.

One of the requirements was that I needed some isolation and configuration between projects, I couldn’t just install a server and be done with it.

Previously I’ve been using Vagrant and VirtualBox, a great fit, very easy to use (most of the time). However, for Magento2 I’ve realised that it was heavy enough on its own, it was making me run out of resources fast.

Also, I wanted it to be easy to use, I don’t like to have to remember and type out a 3 word command, I just want to press some tabs and get it over with.

The requirements

There were some specific requirements:

  • nginx config – should work out of the box, Magento configuration isn’t very small, I wanted to make use of it with ease;
  • SSL – the domain has to also work with HTTPS, mostly because some APIs require it, the certificates don’t need to be valid;
  • bash – the Magento command should work as the system user, not as root (as containers usually do). This is required, because I don’t want the files generated by Magento to be generated as root (and therefore only removable with root rights);
  • xdebug – must work out of the box and be easily integrated with an IDE.

The implementation and usage

Magento2 offered a Docker container to work with. I will not say anything about it, since it wasn’t at all something I needed.

My main source of inspiration was: The project changed a lot since I’ve started, so I definitely think you should check it out.

The starter point is:

The relevant files are:

  • magento2 – it should contain a folder html with the project
  • dkc_short – it can reside anywhere, but it should be added to the files ~/.bash_profile or ~/.bashrc, this file contains shortcuts, it’s not necessary, but I like it because it make my life easier;
  • docker-compose.yml – it contains all the mappings and relevant containers.

NOTE: I think I should point out that the commands on the PHP container run in two ways, as the system user or as root. This is a limitation of the Linux implementation, please make a note of it, as I will refer to it later.

Step 1:

What you should do when starting a new project with an existing Magento2 repository:

$ git clone project_name
$ cd project_name
$ git clone your_own_magento2_repository magento2/html

Step 2 (optional):

Copy the shortcuts to your bash console:

$ cp dkc_short ~/
$ echo ~/dkc_short >> ~/.bash_profile
$ source ~/.bash_profile

NOTE: If you don’t have the file ~/.bash_profile on your computer, just use ~/.bashrc

Step 3:

Start the setup:

$ dkc-up -d

It will take a bit of time the first time, but it will be a lot faster next time you run it.

Step 4:

Run composer install:

$ dkc-php-run composer install

That’s about it.

What is this dkc stuff?

Well, I like to use tabs when running a command, so I added some aliases that allow me to run a Magento command without typing everything, I just type dkc[tab]p[tab]-[tab] and the command. I just love bash autocomplete.

The command list is very simple:

  • dkc-up -d – start the containers in the background
  • dkc-down – stop all containers
  • dkc-mag [command] – run a Magento2 command
  • dkc-clean – clear the cache
  • dkc-php-run – run a bash command inside the php container, like composer in the previous example. NOTE: This command is running as the system user, not as root.
  • dkc-exec phpfpm [command] – this is same as above, but running as root. You should almost always use the command above.
  • dkc-exec [container] [command] – this command needs a bit more explanation:
    • container can be:
      • app – for Nginx server,
      • phpfrm – for php container,
      • db – for database,
      • cache or fpc – for cache containers;
  • the command can be anything that applies to that container, like “bash” or “bash composer”, etc.

I know the commands seem like “one more thing to learn”, but most of the time you will only use the first 4 commands.

How does the magic work?

Well, to see what the above commands translate to, just check the “dkc_short” file.

There are only 2 other interesting repositories:

The repositories are pretty small and not very hard to understand.

If you need to modify anything, just feel free to fork the repositories.

The conclusion

That’s about all you need to know about it, I’ve been using this setup for almost 2 years.

For me, it’s working as a charm and I was able to use Magento2 on ultraportable laptop with 8Gb RAM without any issues.

The (happy) end!

Written by Claudiu Persoiu

29 November 2018 at 3:00 PM

About passion, programming and heating systems

without comments

Don’t be fooled, this post is about programming, system architecture but mostly about using a heating system.

If you don’t know how can you talk about programming without programming you should check out the “The Passionate Programmer” book by Chad Fowler, a great read. The Jazz stories from this book inspired me to write this blog.

The story begins with a new apartment in an old building. Or at least it’s new to me.

The building has its own heating system, very old and extremely inefficient. After a long consideration I’ve decided that it was time to install my own heating system and disconnect from the main building one.

So far, nothing interesting, there are many people that do this, partly because of the added comfort and partly to optimize expenses.

With this being said, I had a project and needed a developer. Or in other words, I had a heating system to build and I was in need of somebody to do it.

I’ve asked around for that “good” developer.

Like in anything else, there are a lot of people that come up with bad solutions. There are devs that make great offers but are unable to finish the project, or they write very bad code that is not scalable and even worst, unmaintainable.

Since what I know about heating systems can be covered by anyone with the patience to google the subject for a couple of hours or so, I wanted somebody I could trust, so I was looking for the passionate kind of developer!

I had a couple of recommendations. The first one told me that he had to put all the pipes close to the ceiling. After convincing him that I don’t want my house to look like a factory full of pipes he said that he will definitely need to replace one of the radiators (at least) because he could not fit a pipe behind it. I could fit my palm behind that radiator, with this in mind I knew I wanted somebody that could fit a pipe behind my radiator.

It was clear that he wasn’t a good developer. A good developer must work with the requirements, the very least a project should be able to respect most of the client requirements, if it doesn’t, there can be several explanations: he can’t because he doesn’t know how or he doesn’t want because he knows it’s hard and doesn’t want to go that extra mile. In some cases that’s not a tragedy, maybe it will be cheaper and faster, and in his case it was. Unfortunately for him, I aimed for quality.

Then there was the passionate developer. He never mentioned anything about not being able to do something, it was always a cost and maybe a consequence. The deal with better developers is that they are more expensive and everything about them is expensive, they will want better servers for hosting, better tools and sometimes more time for things like testing and maintenance. In other words, sometimes the cost is bigger not just then, but also in the log run. A quality project takes time and money.

This is my resulting project:

If you never seen an apartment heating system before you should know that except for the pipes, nothing else is actually required.

It’s all just passion!

For instance: the pump on the lower right, it’s there just to force the water to move faster in the system. Think of it as Redis, it will have a good effect on your system but most systems will happily work without it. Of course, at some point there may be maintenance for that pump and can even result in issues, like this Magento 2 issue: Every system has its own cost.

The expansion tank in the lower left was unnecessary (in the sense that the system already has one built-in), but it’s not a bad idea to have an extra. Think of it as that extra storage space, ram or CPU that you don’t actually use. Your server should never go above certain server loads, that’s the expansion tank you should take into consideration.

The water intake filter is like your firewall, you need it, it’s your protection, maybe most of the time will be useless but when there will be issues, then you will be glad you have it, because he will have filtered them out.

The good thing with passionate developers is that other developers understand and appreciate their work. That is very important, no matter the industry of the “developer”.

The only one that had anything to comment on the system was the ISCIR certified technician that initialized the heating system (ISCIR in Romania is a special authorization needed for this exact thing). You can tell that he wasn’t passionate, he just wanted to say something bad about it because he wanted to make a good impression on me.

Unfortunately for him, he made some very stupid comments and then he made me a maintenance offer. This guy was the consultant, he didn’t do the project and he doesn’t want to work on it but he definitely wants to make some money on it without actually doing anything.

I guess the conclusion is that no matter the developer, the quality and passion transcends the industry.

Written by Claudiu Persoiu

8 August 2018 at 8:47 PM

Posted in Diverse,Web stuff

Tagged with

The Books for Zend Certified Engineer 2017

without comments

I have recently passed the Zend Certified Engineer 2017, certification offered by Rogue Wave.

While experience is the most important thing for a certification, books are the best way to expand your knowledge, in my opinion.

Since a certification is something very specific, there aren’t a lot of books.

Before we begin, keep in mind that the most exhaustive resource for learning PHP is the manual, always take a look on all the classes and functions available.

Zend Certified Engineer Exam Study Guide $20

I’ve bought this book just so I can review it and earn my right to have an opinion about it.

The study guide was introduced with the certification for PHP 5.3 and it was a free resource. The quality of the book has updated continually, but never by a lot and at some point it was no longer a free resource.

Over time I kept hoping that it will become better and better, but it didn’t. It’s a short book with a big font and lots and lots of misspellings and errors.

The only good part about it is that it has several questions for each chapter, other then that it’s not a very good resources and it’s not worth the $20, probably about $5 would be closer to a fair price since with every version they just update it a bit and adding the new features of the language. The questions don’t even seem to change a lot over time.

So if it’s the only book you are going to buy, go for the next one.

PHP 7 Zend Certification Study Guide – Andrew Beak – Apress 2017 – $19.99

The book is exploring all the sections for the certification in more depth, it has over 300 pages.

It feels more like a programming book and less like a guilde. The quality of the book is way better than the official guide, but that’s not hard at all considering that the guide set such a low bar.

There are also questions on all the chapters which is a good way to practice your knowledge in an exam format. The questions just feel a bit more soft than the guide, the exam itself has a lot of very tricky questions.

Of course this book is not exhaustive, you can’t really cover all the subjects in depth in a single book, and even if it did, then you would probably not have time to read it anyway.

If you are going to buy a single book, I would recommend this one, since it’s an actual book, not a very high level (and low quality) guide.

Zend PHP 5 Certification Study Guide, Third Edition – php[architect] – $22

If you think that I’ve got the version wrong, well, what can I say? you are right!

I wanted to add this book here because for me it was the best Study Guide. Over the years there were various study guides but this one I liked the most.

For the PHP 5 certification there wasn’t an official certification from Zend, only the first edition of this book. It was just a nice programming book to read. Sometimes, when I want to remember something that I haven’t used a lot, or at all, I will read the chapter in this book.

When I took the PHP 5.5 certification, (because I just had to collect them all) I was happy to find out that there is a new version of the book.

It’s basically the same book but updated to cover the new certification. It may be that I like Davey Shafik’s style of writing.

The book feels a bit more dense in terms of information, for me it just feels more like a study guide then the previous book.

However this book does not have questions and is no longer up to date.

If you have the time you can give it a read anyway, I think it is a good general PHP book.


Not a lot of books for this certification, but if I had to pick only one, it would be the “PHP 7 Zend Certification Study Guide” from Apress.

Unfortunately there isn’t a lot of competition, because the “Zend PHP 5 Certification Study Guide, Third Edition” from PHP Arch is no longer up to date and the official guide just isn’t worth the $20 price tag.

If your boss will buy the books, I say go all in! After all, the cost is: $20 + $19.99 + $22 = $61.99! Not bad for three programming books!

Written by Claudiu Persoiu

12 June 2018 at 8:44 PM

Posted in PHP

Tagged with , , , ,

Why is a language like PHP so popular?

without comments

Yes, PHP is probably the most popular programming language on the server side for the web at the moment. And it’s probably going to be for a long while…

It’s probably the best!

That’s not really it, just read on…

Why is it so easy to learn?

First of all, it’s the syntax. It’s not the best syntax out there, the reason is actually that it’s the most popular! I knew most of the syntax before I even looked at a “hello world” example, why? I’ve studied C in school, and people coming from Java had the same feeling.

Let’s see a few programming languages that are using this syntax: Java, C#, JavaScript, Perl, Go, PHP and, of course, C and C++, and between them you have most of the market and platforms.

Unlike (most) other languages listed above, it’s a lot easier to get started! You don’t have to create a main function, a class or anything else to get started, you just get down to it!

The good part is that you can make it as complex as you want (or need) to, these days there are Java-like classes and interfaces, horizontal inheritance with traits, functional programming with closures and anonymous functions, generators and, if you really are a hipster, even goto.

But PHP doesn’t sound so bad…

Now, there is an important detail that must be mentioned, PHP didn’t become popular because of the features mentioned above, but the features came to it because of its popularity, even “goto”. I don’t know why somebody would add goto to a language more than 10 years after its creation but that’s none of my business, nor is it important for the subject at hand.

The language became really popular with versions 3 and 4. By the time a decent OOP model is introduced, it had already gained a lot of popularity! Now, especially more so since PHP 7 is looking a whole lot more like a decent programming language, but 10 years ago it wasn’t the same story.

But node.js will probably surpass it!

Right… remember Ruby? Python? And others…

If you think about PHP, it has a major advantage and disadvantage, is very much as stateless as it can be.

With PHP you feel like it doesn’t know anything about the world, you have to bootstrap everything all the time. The advantage is that if you have a memory leak, most of the time, it’s ok. The current user will finish and everything will get cleared and nobody will have to care. And PHP had a lot of memory leaks when it started to became very popular and nobody seemed to mind.

When people started making cron jobs and other long running processes, this becomes a big issue and the need for features like explicit garbage collection became a must. That was added with version 5.3, which was released in 2009.

Normally, continuous running should not be a disadvantage, so the moral of this story is that it’s not necessarily an advantage either.

Nobody is going to build the next Google on it, but how about some blogs? Or even better, some shopping carts?

It just doesn’t have what it takes for something like the Google search engine and that’s ok because it was just not designed for that.

Another example for “not necessarily the best will win” is that WordPress is the most popular blogging platform.

Even more, people are using WordPress for all kind of weird things, basically you can do anything with it! And you should not blame the people using it, it’s not like you look what’s in your microwave when you buy it, you just want to have a warm meal. People forcing the limit of WordPress are doing the same, they can do what they need, so there is no good reason not to.

Should I ever use it?

Let’s take a couple of positive examples: Yahoo! and Facebook.
Yahoo! has been using it for a very long time on some of their products and in combination with other programming languages.

Facebook has been using it since the beginning because it is so easy to learn. They have been pushing the limit of the language in several occasions with: HipHop for PHP and more recently Hack, which was a very important motivational drive and a big influence for PHP 7.

Why all that trouble for a language? Because it’s just so easy to learn and use!

Usually, it’s a lot more important to release “now” then to have something “perfect” later, especially on the world wide web.

There is a very good reason why a language that is definitely not “the best”, but it is easy to learn is so popular online, while a programming language like C, which is way harder to learn, is more popular offline than online.

In other words, while definitely not being the best, PHP is most of the time just good enough.

Written by Claudiu Persoiu

19 October 2017 at 8:46 PM

Posted in PHP

Tagged with

Guide to motivating programmers OR How to provide coders with a real challenge

without comments

motivationIT is probably one of the most dynamic fields in the job market and people are a very important yet limited resource.

And yet people come and go quite often, and most of them are complaining about the lack of motivation.

This problem will end now with this simple guide for motivating programmers and not only them!

Programmers need challenging problems, they keep saying all the time that they need more challenges, don’t they?

Give your coder a true challenge! Make him get specifications from a client that doesn’t know what he wants, let him struggle, you could even try leaving him do this by himself, let him feel the adrenaline of a true challenge!

It must be noted that a programmer is nothing more than a repressed Project Manager, give him a chance to express!

However, you should not stop with that, because there is no greater challenge than to work on multiple assignments at the same time. A good idea for raising complexity is to offer him the chance to work on multiple projects in parallel. Give him the opportunity to have his mind busy with different things, diversity is always good, isn’t it?

A little secret for exploring diversity at its fullest is for him to have different roles on different projects, maybe have him alternate a project that requires management with one that requires maintenance. It’s a shame to not have enough diversity!

Is he writing a lot of code? You have to do something about it! When a programmer is focused working on something, keep in mind that he’s actually desperately crying for attention. You have to do something to break this vicious circle, it’s your duty as a manager to save him!

You have to find a way to interrupt him: try with a few meeting, they always help. If he is looking eager to get out of a meeting, it’s just a sign that he wants to get into the next one!

In time, you’ll see that the better a developer is at writing code, the more he’ll wish not to have time for it anymore. However, he’s not going to tell you this, or, even if he does, it’s because he really doesn’t know what he truly desires.

And eventually, if he likes to write code so much, maybe you should take it to the next level and get him to test it on his own. After all, if he wrote it, who would know better how to test it?
There is one more thing to mention in the context of a challenge. When a programmer is coming to an interview, he will always say “I want to learn new things”. How do you apply in real life this desire? Simple, you have to make him work on something that he doesn’t know anything about! This way you will present him with something that’s at the same time new and challenging. Is he a PHP programmer? Good for him, now give him a Java project! Is he a Java programmer? Nothing could be simpler, give him something to do in Objective-C! Maybe sometimes it will be hard to find new technologies the are completely unfamiliar to him, but a programmer’s happiness is not an easy thing to obtain!

Try not to fall in the trap of offering salaries that are correlated with the position he applied for. Don’t worry, the salary is confidential, nobody talks about it. Especially not when he recommends a friend, he will definitely not inquire about the salary that was offered to said friend, if confidential. It would be a lack of professional ethics to try to find out, you really don’t have to worry about it.

And speaking of interviews, maybe you think that hiring processes should lead to meaningful results and qualified candidates. You couldn’t be more wrong, because what do you truly need? Like I’ve said before: Diversity! Not long ago I’ve read an article about some chinese companies hiring beautiful women to make the work environment more enjoyable and stimulating. You have to always find ways to integrate the new tendencies, and if the only open positions that you have are for technical jobs, you have to work with what you have. Maybe you think hiring based on looks can be problematic sometimes, especially when the jobs are technical. In this cases you have to realise that motivation has to be designed at a large scale. Yes, maybe some teams will require more time to adapt to this kind of new team members, however if that person has done something worth mentioning in any way, it means you’ve done a great job!

Let’s not forget about trainings and conferences. When did you last talk with a programmer saying that he doesn’t want to go to trainings or specialised conferences?

Motivation has to be maintained like any other desire. There are two components to it: wishing for something, and not least, not getting the something that is desired. If you wish for something it’s because you don’t have it yet, right?

If you didn’t already understand, the solution is simple: don’t let him have the something that he wants, so that he will be perpetually motivated to work hard in order to get it. He wants a training? Organise the training then make sure you are sending there the persons that are the least interested in it. He will see that he has a chance, but it wasn’t his turn right now.

Maybe this will sometimes be a challenge for management, but with a lot of carefulness it can be made possible! Keep the desire alive!

Another advice in regards to trainings: to keep the desire alive it is useful to send him to trainings in which he isn’t interested, but in which somebody else is interested. This way you will always have employees interested in trainings and having trainings, and also motivated!

When he is working he shouldn’t be able to see the usefulness of his work, it is very important. If he will see it, how can he be motivated still? It’s like climbing a mountain, no climber is motivated to go on if he knows how high he already is. You must be careful not to let him know where he is standing: keep those statistics and progress reports well hidden, because if he gets to have a feeling of ending, of target reached, of goal achieved, how would it be a true challenge then?

And don’t forget to not show him trust, he must build it for himself. You should provide him with a target and then keep it just a target. Indeed, a target achieved provokes pleasure and the satisfaction of a job well done, but is that what you want to show? Think about it, each time you congratulate him for his work will he be more motivated? Why try to stimulate him to improve when you can stimulate him to reach a target that he can’t. Don’t show him you trust him, let him struggle, let him feel the challenge.

In Romania we have a saying: “the known road is the shortest”. Regardless, some try to automate rudimentary tasks. Basically they are trying to eliminate the known road. Actually, if you prevent him from automating processes you are helping him remain busy, to have a busy mind, and a busy mind is an active mind!

And, secondly, with this approach you are helping him maintain his work place. Maybe he doesn’t realise it, but he will lose his purpose when he will remove exactly the tasks which, eventually, he knew so well, because he made them so many times… Programmer or not, he mustn’t be replaced by a robot!

Thirdly, it’s the satisfaction you get of walking the known road – don’t let him divert from it, because, it’s so well known that it should be finished quickly enough.

And never forget that you must teach him the importance of management. He must see that you can help him, that you are the key to his success and happiness. To show this it is very important to trust yourself. Try to organise a party and don’t invite him. Nothing says more “I could’ve been there” than not being there.

Another way is to organise a business trip in an exotic location. Don’t worry, it’s not important to actually have something to do there. While you’re there, don’t forget to have fun, it is very important for him to know that you’re enjoying yourself. When it gets though, you have to remember that you’re not doing this for you, you are doing your best to have fun for his and his imagination’s benefit!

Maybe some suggestions will not work for everybody, but you have to combine as many of them as possible to have a real success and keep your programmers truly motivated!

Don’t try to identify this utopian world, because any resemblance with the daily reality is purely coincidental.

Written by Claudiu Persoiu

4 April 2016 at 9:39 PM

Posted in Diverse

Tagged with ,

PHP6, just another story… (PHP6 and the books)

without comments

It’s finally over, the war has ended. PHP7 was released, and PHP6 will remain only a legend, a story of the version that never was.

After a while I don’t think anybody expected it to be released anymore. Afterall, it’s been almost 10 years, out of the entire lifetime of 20 years for PHP.

But how can such a highly anticipated version never show up?

Let’s begin from the start, when, almost 10 years ago, PHP was becoming a “serious” language. After the PHP5 version, the object module was finally having specific visibility for methods and properties, objects were no longer copied, but sent by reference. These changes triggered a multitude of object oriented frameworks, which were now also targeting the enterprise environment, not only small websites.

In all this context there was a problem, internationalisation.

This is how PHP6 appeared, it should have used natively Unicode, more specifically UTF-16. This way everything would have been processed in a unique and international format.

The project was started and maintained by Andrei Zmievski. Maybe today not many people know much about Andrei, but 10 years ago he was very popular for projects like Smarty and PHP-GTK.

After a few years the project got stuck, and then PHP5.3 appeared. Even though it was bringing important changes, it wasn’t a major version because there was still hope that one day there will be a PHP6.

There were however some people full of hope! And even more, they were ready to cash in on this bright hope!

Either authors or editors, they knew one thing when it came to books (especially technical ones) that they have two properties: a target audience and a period of relevance.

Let’s analyse the results.

PHP_6_and_MySQL_5_for_Dynamic_Web_Sites_Visual_QuickPro_GuidePHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (3rd Edition) (Peachpit Press – 2008) – Larry Ullman

Larry wrote several books, including: “PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)” and of course “PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide”.
Coincidentally, I had the opportunity to flick through all three books, but not in chronological order, I just randomly started with “PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)”. What is interesting is the striking resemblance, basically a 6 was added to the title and the rest is almost the same.

In general, all books from Visual QuickPro Guide collection are targeting beginners, and from an edition to the next things don’t change too much, they are only adjusted.

Professional_PHP6_Wrox_Programmer_to_ProgrammerProfessional PHP6 (Wrox 2009) – Ed Lecky-Thompson, Steven D. Nowicki (Thomas Myer)

It’s interesting that in the cover image there are three authors, but on and on Wrox there are only two names, that’s why I wanted to single Thomas Myer out.

This makes me wonder if Thomas Myer purposely asked to be removed or were there other factors at play? It’s strange to have your name on the book but not in the author listing of the publisher’s website.

Not coincidentally there is also a “Professional PHP5” – Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove.

PHP_6_MySQL_Programming_for_the_Absolute_BeginnerPHP 6/MySQL Programming for the Absolute Beginner (Cengage Learning PTR – 2008) – Andrew B. Harris

It’s truly a strange book, there isn’t a PHP5 equivalent, and even more, on the writter’s website there isn’t any other PHP book.

Could there be a reason for that? In Larry Ullman’s case it’s just another covered subject, additional audiences targeted, but in this case it’s just strange, a single author, a single PHP book for a ghost version, published in 2008.

PHP_6_Fast_and_Easy_Web_DevelopmentPHP 6 Fast and Easy Web Development (Cengage Learning PTR – 2008) – Matt Telles, Julie C. Meloni

It wasn’t very difficult to also find: “PHP Fast & Easy Web Development” – Julie C. Meloni.

What is strange about this book is that Matt Telles doesn’t have any other PHP books, even though he wrote some for Python, C# and C++.

Something tells me he was brought in just to adjust the original book for the new context. I thought it was just a personal supposition, but upon searching on Google I found he is also Technical Reviewer for “PHP 6/MySQL Programming for the Absolute Beginner (Cengage Learning PTR)”, book that I covered above.

Beginning_PHP_6_Apache_MySQL_6_Web_DevelopmentBeginning PHP 6, Apache, MySQL 6 Web Development (Wrox – 2009) – Timothy Boronczyk, Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz

The original book was obviously: “Beginning PHP, Apache, MySQL Web Development” – Michael K. Glass, Yann Le Scouarnec, Elizabeth Naramore, Gary Mailer, Jeremy Stolz, Jason Gerner.

Michael K. Glass and Gary Mailer got out and in their place Timothy Boronczyk went in.

PHP was a very fashionable language in that period, and PHP6 was very awaited.

But how can something like this be? How can there be some many books from important publishers for a programming language that never existed? The answer is simple: greed.

There are two possible explanations: either the author tried to seize the market with a new version, or the publisher wanted to force it, in the hope of coining the market. Probably the hope was that when PHP6 was coming out, they would have already have the books ready for shipping.

This was another reason for not to rename PHP NG version into PHP6, it would have meant that a PHP version would have come out with a series of books already published, and what’s more, they were published for several years already.

I bet that when the vote between PHP6 and PHP7 was happening there were some very hopeful editors.

And now I wish you all, a pleasant reading!

Written by Claudiu Persoiu

22 February 2016 at 9:11 PM

Posted in PHP

Tagged with ,

Yet another PHP 20 blog…

with 2 comments

PHP 20 party

20 years ago, Rasmus Lerdorf announced a new tool called PHP.

My first encounter with PHP was in 2004, eager to make my HTML pages more dynamic. Somebody recommended PHP because it was very easy to use.

Because the installation seemed pretty complicated, I’ve used PHP Triad. In a few minutes I was ready to begin my experiments and I was fascinated by the instant feedback.

Back then, I was a student and my courses were based on Pascal and C/C++. I didn’t like Pascal because it was already outdated and C had a very complicated memory management system and instead of concentrating on the logical thinking I had to focus on allocating resources.

This new language, to me, had a syntax similar to C, but without the headache of resource management. Eliminating this impediment, I got to develop my logical thinking and even to become a better overall programmer, not only in PHP.

In that period, PHP wasn’t considered by many a “serious” programming language, more like one for beginners. At one of my first interviews, the employer told me on a nagging tone: “PHP is a toy, join us and you’ll learn real programming and something serious, like FoxPro!”. That was one of the moments that made me decide to become a PHP programmer.

11 years later, I can still say that PHP is the language closest to my heart, and I probably wouldn’t have had a career as a programmer without it.

What was your first interaction with PHP?

Written by Claudiu Persoiu

8 July 2015 at 3:02 PM

Posted in PHP

Tagged with

Collecting Hack

without comments


Something I didn’t approach in my last blog is collections. Hack comes with a variety of collections for organizing data.

Data structures represent a fundamental part of a programming language, because they will determine the information flow in the application.

PHP up to version 5 had a single type for data collections, called “array”. This data type can have three uses: arrayhash table, or a combination of the two.

To facilitate the construction of new structures, a number of  iteratori were introduced in PHP 5. Unfortunately, the resulting structures had the purpose of accessing objects in a similar fashion with arrays.

Not until PHP 5.3 data structures like  SplStack and many others that are truly different were introduced.

However, structures like vectors and tuples were never natively introduced. They can be built, but it is neither simple, nor intuitive.

HHVM’s Hack comes with a different approach, a series of  native collections that are ready to be used.

Collection types

The list of collections is:

  • Vector – indexed list of items,
  • Map – dictionary type hash table,
  • Set – list of items that only stores unique values
  • Pair – a particular vector case that only has two elements.

Vector, Map, and Set also have immutable (read-only) equivalents. These are:  ImmVectorImmMap and ImmSet. The purpose of these data types is to expose the information for reading purposes and not allow modifications. An immutable collection can be directly generated using the constructor, or using the methods: toImmVector, toImmMap and respectively toImmSet.

Even more, there are a series of abstract classes to help easily implement similar structures:



The advantage of a vector is that it will always have the keys in sequence and the order of the elements is not going to change. When it comes to arrays, there isn’t any simple way to check if it should behave as a hash table or as a vector. For vectors, unlike for hash tables, the key value is not relevant, only the sequence and the number of elements are important.

Let’s take an example:


function listVector($vector) {
     echo 'Listing array: ' . PHP_EOL;
     for($i = 0; $i < count($vector); $i++) {
          echo $i . ' - ' . $vector[$i] . PHP_EOL;

$array = array(1, 2, 3);


// eliminating an element from the array


The result will be:

Listing array:
0 - 1
1 - 2
2 - 3
Listing array:
0 - 1

Notice: Undefined index: 1 in ../vector.hh on line 6
1 -

The reason is very simple: count returns the real number of elements, but the index is not guaranteed sequential. When the second element of the array was removed, the number of elements was reduced by one, but the index with value 1 was no longer set and the last index is equal to the size of the array, so it will never be reached.

Let’s take the same example but using a vector:

$vector = Vector{1, 2, 3}; 


// eliminating an element from the vector


Like we anticipated, the result is:

Listing array:
0 - 1
1 - 2
2 - 3
Listing array:
0 - 1
1 - 3

It is worth mentioning that “unset” can not be used, because it is not a key to be eliminated, but the element itself, and the next value in the vector will take its’ place.

Another important thing to mention is that when an index doesn’t exist, an exception of the type “OutOfBoundsException” will be thrown.

Some examples that will trigger the exception above:


$vector = Vector{1,2,3,4}; 

// it will work because the key with value 1 exists 
$vector->set(1, 2);

// it will not work because the key with value 4 doesn't exist yet
$vector->set(4, 5);

// it will not work for the same reason as above
$vector[4] = 5;

// for addition only method that don't provide the key work
$vector[] = 5;

// or
array_push($vector, 5);

For accessing elements, the “OutOfBoundsException” problem remains the same. For instance, if the index 10 doesn’t exist:


Another more special case is when the element doesn’t exist, but the method “get” is used:


The example above will not generate an error, but the result will be “null” when the key doesn’t exist. I find this strange, because an element with the value null can exist in the vector, and the result will be the same.

To avoid the confusion between undefined elements and elements that are null, there is a special method to check if the key exists:


Removing elements from the vector is done with:


Or to remove the last element:



In a hash table, unlike a vector, the order of the elements is not very relevant, but the key-value association is very important. For this reason, a Map is also called a “dictionary”, because you can easily get from a key to a value, since they are “mapped”, hence the name “Map”.

The HHVM implementation will also retain the order in which the elements were introduced.

In PHP, the equivalent of a Map is an associative array.

Unlike Vector, Map needs a key that will permanently be bind with the element, even if new values are added or removed from the collection.

The functions array_push or array_shift will not work for Map, because a key is not sent and the key-value association would not be controlled:


$map = Map{0 => 'a', 1 => 'b', 3 => 'c'};

array_push($map, 'd');

array_unshift($map, 'e');


Will generate the following result:

Warning: Invalid operand type was used: array_push expects array(s) or collection(s) in ../map.hh on line 5

Warning: array_unshift() expects parameter 1 to be an array, Vector, or Set in ../map.hh on line 7
object(HH\Map)#1 (3) {
  string(1) "a"
  string(1) "b"
  string(1) "c"

As you can see, the elements were not added and each of the cases generated a Warning.

The actual insert can be done using:


$map = Map{0 => 'a', 1 => 'b', 3 => 'c'};

// adding an element using the array syntax
$map['new'] = 'd';

// adding an element using the method provided by the structure
$map->set('newer', 'e');


The result will be:

object(HH\Map)#1 (5) {
  string(1) "a"
  string(1) "b"
  string(1) "c"
  string(1) "d"
  string(1) "e"

Unlike Vector, because the element is closely linked with the key, unset is a viable method for removing an element:


The structure also has a method for removing the element with a particular key:


For this case, none of the options will generate an error, if the key is not set.

The “OutOfBoundsException” exception is also found here for keys that are not defined, and just like for Vectors, there is a method to test if the key exists:


Similarly to Vector, there is a method that will return true if the key exists and null if not:


To make sure that a “OutOfBoundsException” will not be raised, a loop over a Map should not be done using “for” , but rather “foreach”.

Because the vector’s method “pop” does not use a key, it isn’t present in the Map structure.


Set has the purpose of keeping the values unique. For this structure, the values are restricted to the scalar types: string and integer.

The interface for this structure is much simpler than Vector and Map, because the purpose is a lot more limited.

For Sets the key can not be accessed, but it is relevant in a special way.

Let’s take an example to illustrate this:

$set = Set{'a', 'b', 'c'}; 

foreach($set as $key => $val) {
     echo $key . ' - ' . $val . PHP_EOL;

The result will be:

a - a
b - b
c - c

The key and the value are identical, a clever way to keep unicity.

However, the process is transparent, fact that allows adding elements without a need for a key:


$set = Set{'a', 'b', 'c'};

array_push($set, 'd');

array_unshift($set, 'e');

$set[] = 'f';


There will be a result similar to the one from vectors:

object(HH\Set)#1 (6) {
  string(1) "e"
  string(1) "a"
  string(1) "b"
  string(1) "c"
  string(1) "d"
  string(1) "f"

Even though new values can be added using the “[]” operator, they can’t be referenced using this operator:


$set = Set{'a', 'b', 'c'};

echo $set['a'];

It will generate the following error:

Fatal error: Uncaught exception 'RuntimeException' with message '[] operator not supported for accessing elements of Sets' in ../set.hh:5
Stack trace:
#0 {main}

For removing elements only the native method (remove) and methods that don’t require a key can be used:


$set = Set{'a', 'b', 'c', 'd'}; 





The result will be:

object(HH\Set)#1 (1) {
  string(1) "c"

Unlike Vector and Map, the “remove” method will receive the value to be removed, not the key.

For Set there isn’t any access key for elements, therefore about all we can do is to check if an element exists, using “contains”:


The method will return a bool showing if the element exists or not.


A pair is a collection with two elements. It can’t have more or fewer. Just like in Vectors, the elements are indexed using a key that in this particular case can have only two values 0 and 1.

There aren’t a lot of things to be said about this data structure, because the elements can not be removed, added or replaced. This is the reason why it doesn’t have an immutable equvelent, because the structure itself is not flexible:


$pair = Pair{'a', 'b'}; 

foreach($pair as $key => $val) {
     echo $key . ' - ' . $val . PHP_EOL;

The result will be:

0 - a
1 - b

A very simple structure for a very simple purpose.

Common ground

Almost all structures presented above have few common methods and behaviors. Almost all, because Set and especially Pair are more restrictive through their nature and lack some features which Vector and Map have.


It’s a filtering function that comes from functional programming. The purpose is to filter a data structure and to generate a new one of the same type. The exception is Pair, because of the number of elements restriction. The equivalent in PHP is array_filter.

Vector and Map have two methods: filter and filterWithKey. These methods take an argument of type “callable”, in other words a function:


$vector = Vector{'a', 'b', 'c', 'd', 'e'}; 

// eliminate the element with value 'a' 
$result = $vector->filter($val ==> $val != 'a');

// eliminate every other element using the key
$result2 = $vector->filterWithKey(($key, $val) ==> ($key % 2) == 0);


The result will be:

object(HH\Vector)#1 (5) {
  string(1) "a"
  string(1) "b"
  string(1) "c"
  string(1) "d"
  string(1) "e"
object(HH\Vector)#3 (4) {
  string(1) "b"
  string(1) "c"
  string(1) "d"
  string(1) "e"
object(HH\Vector)#5 (3) {
  string(1) "a"
  string(1) "c"
  string(1) "e"

As you’ve noticed, the result of the “callable” function is treated as a bool and according to this the elements are added to the resulting structure.

Map has an identical behavior with Vector, the only difference is in the nature of the keys.

Something interesting is that a collection can also be immutable, because the operation doesn’t modify the original structure, but the it will also have the type of the original structure:


$vector = Vector{'a', 'b', 'c'}; 

$vector = $vector->toImmVector();

// eliminate the element with value 'a'
$result = $vector->filter($val ==> $val != 'a');


The result will be:

object(HH\ImmVector)#2 (3) {
  string(1) "a"
  string(1) "b"
  string(1) "c"
object(HH\ImmVector)#4 (2) {
  string(1) "b"
  string(1) "c"


Pair also has the same functions as Vector and Map, but the behavior is not identical, because of the fact that Pair can only have 2 elements, no less, no more. For this reason, when a Pair is filtered, the result will be ImmVector, a similar structure with Pair but with a variable number of elements:


$pair = Pair{'a', 'b'}; 

// eliminate the element with value 'a'
$result = $pair->filter($val ==> $val != 'a'); var_dump($result);

The resulting structure will be:

object(HH\ImmVector)#3 (1) {
  string(1) "b"

Set only has the “filter” method, because, as was demonstrated earlier, the keys are identical with the values. If it had had a method with keys, it would have worked the same.


Another function coming from functional languages is “Map”. This aims to modify the values of a structure using a function, the resulting structure having the type of the source. In PHP, the equivalent is array_map.

Similarly with filter, Vector and Map have the common methods: “map” and “mapWithKey”. In this case also, they take a “callable” as an argument:


$vector = Vector {'a', 'b', 'c'}; 

$result = $vector->map($val ==> $val . $val);

$result2 = $vector->mapWithKey(($key, $val) ==> str_repeat($val, 1 + $key));


The result will be:

object(HH\Vector)#1 (3) {
  string(1) "a"
  string(1) "b"
  string(1) "c"
object(HH\Vector)#3 (3) {
  string(2) "aa"
  string(2) "bb"
  string(2) "cc"
object(HH\Vector)#5 (3) {
  string(1) "a"
  string(2) "bb"
  string(3) "ccc"

The result of the “callable” function is the new value of the element in the structure.

Just like with “filter”, an immutable collection will result in a new immutable collection.

Also similar with “filter” is the fact that the “map” function applied to a Pair will result in an ImmVector:


$pair = Pair{'a', 'b'}; 

$result = $pair->map($val ==> $val . $val);


Will result in:

object(HH\ImmVector)#3 (2) {
  string(2) "aa"
  string(2) "bb"


Some of the elements can be converted to different types:

from \ ro Vector Map Set Pair Array
Vector x x x x
Map x x x x
Set x x x
Pair x x x x
Array x x x x

There are several structural restrictions to the table above:

1. Any structure that is getting converted to Set must only contain scalar values of type int and string:

(Map{})->add(Pair {'a', new stdClass()})

Will generate the error:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Only integer values and string values may be used with Sets' in …

2. When a Map is converted to any other structure, except array, it will loose the keys in most cases.

The conversion from an array to other structures is done using:

$vector = new Vector ($array);

Beside Pair, all structures above have a single parameter that implements Traversable for the constructor.


Hack brings a new perspective over the most popular data type in PHP. Facebook’s reason is a simple one, optimization. If you have a consistent behavior, that particular structure can be optimized. In PHP that’s not exactly possible, because of the fact that an array in PHP can be any type of collection.

From the data structure point of view, I find it interesting to have this kind of data types. In frameworks, there are usually structures that emulate the behavior of the collections introduced by Hack. For instance in an ORM, a collection of objects is usually represented as a vector, because the purpose is to iterate over its’ values. An object that represents the values of the fields from a table will be a Map like structure, because the value of the field is closely related to the field name.

I find it very interesting not only that now we have this structures, but also that we have the interfaces to implement new ones.

I hope Hack will influence PHP to bring purpose specific structures into the language.

Written by Claudiu Persoiu

30 May 2014 at 11:50 AM

Posted in PHP

Tagged with , , , , , , ,

Hack programming language, the PHP apocalypse?

without comments



About a month ago, Facebook released  the Hack programming language.

Since then, apocalyptic articles related to this language and to how it will replace PHP have appeared everywhere. The title of this article was inspired by  “Will Hack Kill PHP?“.

What is even more strange to me is that it was followed by a wave of negative assessments related to PHP, apparently Hack “fixes” the previously mentioned language. In my opinion, the language has to be “broken” in the first place to be “fixed”.

Off-course PHP has many drawbacks, like any other programming language, but there must be a good reason why it is the most popular language for the Web. After all, Facebook used it for a long time, and now it is not replaced, they are improving it… aren’t they?

One thing is for certain, it is probably one of the least inspired names. When you search for “Facebook hack” you will find anything else but this programming language…

About Hack

Hack is running on HHVM. HHVM is Facebook’s try to optimize the PHP language with Just In Time complication, the latest approach in optimization of the language. Basically, Facebook is trying to reduce their costs by optimizing the language interpreter, and now with a new language all together. After all, if we think about Facebook’s infrastructure, it’s normal for them to do this, even a relatively minor optimization will lead to a consistent cost reduction.

Initially, I thought it was an “improved” version, but it seems like it is another language altogether, basically it’s a PHP with something extra!

A small tutorial of the language is at:

The tutorial doesn’t cover all the features of the language, more details are at:

Practically, about all the features that differ in Hack from PHP are optional. You can almost write PHP and it will work. Contrarily to expectations, not even specifying the type of the input/output of the variables is not required.

Because after all it’s a different programming language altogether, I’m not going to go into much details on all the new features. That is more the purpose of a book, not an article.

I only want to point out a few features that I found interesting.


Strangely, at least at the begging, at runtime the type of the result, even though it is sent, is not necessarily interpreted.

Let’s take an example:


function a($a): void {
     return true;

echo a('a');

This example will have the output… 1.

Basically, at runtime only the input type is checked, not the output one.

To run the Typechecker in the current directory, an empty file must be added:

$ touch .hhconfig

Then run:

$ hh_client

As you can see, the data types are checked in a separate step beside runtime.

At the manual execution of the Typechecker, it will find all the inconstancies. The purpose is for it to identify the problems before the runtime, for instance when you edit a file, not when the app is actually running.

The Typechecker output is:

../test.php:4:9,12: Invalid return type
../test.php:3:17,20: This is void
../test.php:4:9,12: It is incompatible with a bool

Unfortunately, there is still a lot of work to be done with this feature. If we try to validate only at runtime, using “type hinting” like in PHP, the function becomes:

function a(int $a): void {
     return true;

echo a('a');

The output of the Typechecker is not changed, but upon execution the result will be:

Fatal error: Argument 1 passed to a() must be an instance of int, string given in ../test2.php on line 5

Basically, the Typechecker is doing what the type hinting is not and the the latter is now also receiving scalar type arguments.

Even if the only change was to add scalar arguments checking, I would have still considered it an important improvement.

Lambda operator

It is a syntax that is more popular with the functional languages.

An example:


$sqr = $x ==> $x * $x;

echo $sqr(5) . PHP_EOL;

Off course the result will be 25.

I find it a very interesting and clear way to represent small logic.

In the new syntax, a function can also return another function:

$add = $x ==> $y ==> $x + $y;

$result = $add(1);

echo $result(2) . PHP_EOL;

The result will be 3.

If a variable from inside a lambda expression doesn’t exist in the scope of the function definition, then it will be retrieved from the environment in which the expression was declared:

// variable in the current scope
$z = 5;

$addZ = $x ==> $x + $z;
// change the variable in from current scope
$z = 6;

// perform the add
echo $addZ(1) . PHP_EOL;

The result will be… 6!

The equivalent in PHP is:

$addZ = function ($x) use ($z) {
     return $x + $z;

The value of $z will be retrieved for the environment where the function was defined, not as a reference to the variable.

Off course that’s not the case when the variable from the outside of the function is an object, in this case it will be passed by reference:


class a {         
     public function __construct(public string $x) {}         
     public function __toString() { 
          return $this->x; 

// variable in the current scope
$z = new a('Claudiu');

$addZ = $x ==> $x . ' ' . $z . '!';

// change the variable that will be used for concatenation
$z->x = 'World';

// run the concatenation
echo $addZ('Hello') . PHP_EOL;

The output will be:

Hello World!


Again, a syntax more popular with the functional programming languages. The purpose is to validate a more specific type of structure than an array.

The reason is very good, validate simple data structures. The structures that are getting checked should contain the elements defined in the shape.


// defining a structure
newtype Circle = shape('radius' => int);

// a function that will is using the type of the structure above
function areaCircle(Circle $param) {
     return M_PI * $param['radius'] * $param['radius'];

// a series of shapes that are using the structure
$circle = shape('radius' => 10);
$cilinder = shape('radius' => 10, 'height' => 15);

// a structure that should not work pass as Circle
$sqr = shape('side' => 10);

echo areaCircle($circle) . PHP_EOL;
echo areaCircle($cilinder) . PHP_EOL;
echo areaCircle($sqr) . PHP_EOL;

The output is:


Notice: Undefined index: radius in /home/brand/test.hh on line 6

Notice: Undefined index: radius in /home/brand/test.hh on line 6

A little disappointing, I was hoping that the parameter that doesn’t match the structure will trigger an error, but it passes.

Not even the Typechecker finds anything wrong.

The intention is very good, now we just have to wait for the working version.


Probably Hack will influence PHP, which is normal in the end, it happens all the time with programming languages.

Will it replace PHP? I don’t think so, probably there will be a lot of adopters for cost reduction or a better structuring of the code.

It is not very kely for this language to be successful in the following years other than for projects of medium and large sizes. For small projects usually shared hosting is used, and this generally doesn’t have the latest PHP version, it is even less likely for it to have the latest HHVM. This is probably one of the least interesting arguments, but in the end most of the websites on the web are of small and very small size, they make up the “mass”.

An easier approach to optimization is to only use HHVM. In theory, you don’t have to change anything and the results should be visible immediately! Practically HHVM is not 100% compatible with Zend Engine, but this problem is getting less of an issue with each version. One of the priorities for HHVM is to interpret the code the same way Zend Engine does, but to be much more efficient!

Written by Claudiu Persoiu

29 April 2014 at 10:15 PM

Posted in PHP

Tagged with , ,