Functional approach in PHP

The code snippet below will only work in PHP-5.3+!.
Isn’t it the famous functional programming with functions as first class citizens, closures and other functional bla-bla?

<?php
 
function one($a = 0) {
    return function(callable $b) use ($a) {
        return call_user_func($b, $a);
    };
}
 
print_r(call_user_func(one(1), function ($x) { return $x + 4; } ));

This was created just for fun. As you may see, in place of callable you can put any valid PHP callable, e.g.in a form ['Class', 'method']

Use your imagination for other real things, but also try to stay not cursed later by those who will inherit your untraversable codebase :LOL:

[SOLVED] Upgrading from PHP 5.5 to PHP 5.6 on Ubuntu 12.04 LTS

Recently I had to upgrade PHP version on the build server based on Ubuntu 12.04 LTS to PHP 5.6 At that moment there was PHP 5.5 installed on the server. Everything should have been as easy and promising as described, for example, in this post. Yes, it was going well to the moment

sudo apt-get dist-upgrade

Then dpkg claimed unmet dependencies… You know, as usual. I decided – f…k it, I will remove existing php and everything related. Simple removal with

sudo apt-get remove php*

did not work fine and stuck on removing phpmyadmin.

That guysexited with an error code 10 being reported from its post-remove script. After pretty short googling, I found the solution which worked for me.

The similar shit happened with php5-imagick package, so the solution I used was similar to phpmyadmin removal. Afterwards, I checked for presence anything starting with or inlcuding php:

sudo dpkg -l | grep php

Everything was clean. OK, here we go…

sudo apt-get clean && sudo apt-get autoclean
sudo apt-get udpate && sudo apt-get upgrade && sudo apt-get dist-upgrade

…and still the same error in result:

Reading package lists… Done Building dependency tree Reading state information… Done Some packages could not be installed. This may mean that you have requested an impossible situation or if you are using the unstable distribution that some required packages have not yet been created or been moved out of Incoming. The following information may help to resolve the situation: The following packages have unmet dependencies: php5 : Depends: libapache2-mod-php5 (>= 5.6.16+dfsg-2+deb.sury.org~precise+1~) but it is not going to be installed or libapache2-mod-php5filter (>= 5.6.16+dfsg-2+deb.sury.org~precise+1~) but it is not going to be installed or php5-cgi (>= 5.6.16+dfsg-2+deb.sury.org~precise+1~) but it is not going to be installed or php5-fpm (>= 5.6.16+dfsg-2+deb.sury.org~precise+1~) but it is not going to be installed E: Unable to correct problems, you have held broken packages.

I tried install with -f flag, package-by-package. Nothing worked. The final unmet dependency was:

sudo apt-get -f install php5-json
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
 
The following packages have unmet dependencies:
 php5-json : Depends: phpapi-20121212
E: Unable to correct problems, you have held broken packages.

And here I stuck, as googling at this point didn’t help me at all… I visited the home of php5.6 PPA hoping to find something there. And a brilliant idea came to my mind – I should check APT’s sources list! I saw this files inside /etc/apt/sources.list.d:

/etc/apt/sources.list.d$ ll
total 32
drwxr-xr-x 2 root root 4096 Jan  5 11:12 ./
drwxr-xr-x 6 root root 4096 Jan  5 11:11 ../
-rw-r--r-- 1 root root  138 Jan  5 10:32 chris-lea-node_js-precise.list
-rw-r--r-- 1 root root  138 Jan  5 10:32 chris-lea-node_js-precise.list.save
-rw-r--r-- 1 root root   60 Jan  5 10:32 nginx-stable-lucid.list
-rw-r--r-- 1 root root   60 Jan  5 10:32 nginx-stable-lucid.list.save
-rw-r--r-- 1 root root  134 Jan  5 10:32 ondrej-php5-5_6-precise.list
-rw-r--r-- 1 root root  134 Jan  5 10:32 ondrej-php5-5_6-precise.list.save
-rw-r--r-- 1 root root  126 Jan  5 10:32 ondrej-php5-precise.list
-rw-r--r-- 1 root root  126 Jan  5 10:32 ondrej-php5-precise.list.save

I realised, the problem could be that packages were overwritten from older php-5.5, which apparently was installed manually to the server some time earlier. Once I removed these two files:

ondrej-php5-precise.list ondrej-php5-precise.list.save

and updated apt one more time, I was able to install PHP 5.6 as usual:

sudo apt-get install php5
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  shtool libc-client2007e node-node-uuid libicu52 libicu48 libv8-3.7.12.22 libmcrypt4 libt1-5 mlock libssl-dev libvpx1 libssl-doc zlib1g-dev libgd3 libev4 libv8-dev libc-ares2 libev-dev
  libc-ares-dev
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  libapache2-mod-php5 php5-cli php5-json php5-readline
Suggested packages:
  php-pear
The following NEW packages will be installed:
  libapache2-mod-php5 php5 php5-cli php5-json php5-readline
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 5,575 kB of archives.
After this operation, 18.9 MB of additional disk space will be used.
Do you want to continue [Y/n]? Y

The main idea of this story, before adding new custom repository for PHP, make sure you removed any other old ones! Good luck and happy coding!

HowTo Compile and Install php-5.4.x on Debian OS (squeeze)

Recently I was trying install latest PHP 5.4.x (at the moment of writing this is 5.4.10) version on my stable Debian OS (v6.0-squeeze-32bit).
As far as you may know, stable Debian proposes PHP-5.3.4 as latest available release. I wanted to use embedded into PHP-5.4 web-server.
I was thinking that install PHP-5.4 would be faster process then configuring new virtual host in Apache. Yep, lazy me! 🙂
Well, spoiling the story, I would like to say, that the overall process took 2-2.5 hours from me before I was able to launch my scripts.
To be honest, that was my first time when I was compiling PHP from source, and I feel I got great experience with that!
But okay, let’s look at the process step-by-step.

First of all, you may need install “libxml2-dev” linux library (don’t remember why, though :-)).
Oh, wait! Looks like it needed for you be able to configure you PHP installator.

sudo apt-get install libxml2-dev

I also believe that most web developers need to use DB with theirs scripts, thus they may need PDO php extension.
So, you need to compile PHP with this directive:

configure --with-pdo-mysql --with-mysql

(see http://php.net/manual/en/ref.pdo-mysql.php for more details)

Usually, good devloper uses debug tools extensively. As we all know most popular PHP debugger is a xdebug.
This package could be installed from pecl repository.
Before asking pecl to install xdebug, make sure you have php5-dev package installed in your system. With that package phpize is being shipped, which is required to install PHP extensions through pecl.
If you don’t have php5-dev install, you are going get this error message:

ERROR: `phpize’ failed

and here is your next command:

sudo apt-get install php5-dev

After that, you will be able install xdebug extension with help of pecl:

sudo pecl install xdebug

Next. Let’s assume you work with not only documents written solely in English, but with some other languages (Italian, Portugeese, Russion, Dutch, etc.)
Also let’s assume you need to handle dates written in non-english format (btw, about that look for my article about how one can handle dates written in languages different from English). php5-intl extension comes to help! Here http://php.net/manual/en/intl.installation.php you will find information how to enable this extension:

configure --enable-intl

If PHP’s configurator shows error message

“configure: error: Unable to detect ICU prefix or no failed. Please verify ICU install prefix and make sure icu-config works.”

you need install this Linux package:

apt-get install libicu-dev

These are development files for International Components for Unicode.

Next, it may happen that you need load remote http resources. Usually, that is done with curl (php5-curl).
I was not enable that extension because faced such message:

configure: error: Please reinstall the libcurl distribution

In such case you need install this Linux package:

sudo apt-get install libcurl4-gnutls-dev

(thanks to http://phpconfigure.com/2011/04/configure-error-please-reinstall-the-libcurl-distribution/)

In case if you need to deal with multibyte encodings and use PHP’s functions prefixed with mb_*, you need enable that as well:

configure --with-mbstring

Okay, as I said, after couple hours of fight I was able to finish with this configuration options and directives:

sudo ./configure --with-pdo-mysql --with-mysql --with-curl --enable-intl --enable-mbstring=all

As I said at the beginning, I was hoping that install PHP-5.4 would be faster then configure virtual host. How far away from truth was I!..
But I do not regret about my choice, because now I feel strong in believe that I have mastered some important things and have grown my professional level!

Fell free to add comments and ask things that left unclear for you.

PHPMyAdmin session timeout

By default, the lifetime of the session in a new installation of PhpMyAdmin and ‘cookie’ authorization method is 1440 seconds (24 minutes). Which means that after 24 minutes of inactivity any action brings the user to the login page. To increase the lifetime of the session, you must set the variable

$cfg['LoginCookieValidity'] = 1440 * 60; // 86400 means one day (24-hours)

, say, 60 times. That amount per day.