Tag Archives: php

Hacking PHP

Facebook has launched the programming language HACK as neither a subset or superset of PHP but more a dialect. It adds typing, generics, lambdas and some more features to PHP.
In this blog I will show how some trivial PHP can be translated to HACK and some things I encountered while trying it out.

From PHP…

I start with a simple pure PHP5 class with a constructor and one method that echos the current variable $a. Nothing extraordinary but I want to show what have to be done to convert this code into a hack file.
Continue reading Hacking PHP

Adding users

In a previous post I’ve showed some code to add nodes to Drupal, in this blog I will show that the same can be done with user objects.

$roles = array(
   5 => 'visitor'

$fields = array(
    'name' => $username,
    'mail' => $email,
    'pass' => $password,
    'status' => 1,
    'init' => 'email address',
    'roles' => $roles
//the first parameter is left blank so a new user is created
$account = user_save('', $fields);

// $account will contain the new created user object

The fields are rather simple and easy to understand what they do:

  • name; loginname of the user
  • mail; email address
  • pass; plaintext password, will be hashed on save
  • status; 1 for an active user and 0 for an blocked user
  • roles; an array with the roles to assign to the user. The number and name is different per Drupal installation

So it is rather simple to add an user in Drupal, as it should be ๐Ÿ™‚

The problem with dynamic languages and large projects

Abstract: In large projects it is important to find unused and old code and remove those pieces of code. With dynamic languages this is virtually impossible. It is important to clean and refactor unused parts of a system because this can become a security risk. Also a lot of “noise” is added to the system that makes it more difficult to understand the system.

While working on a large project I have the need to find pieces of code that are no longer in use. With a dynamic language and specific PHP this is virtually impossible and below I will give an explanation why that is. Continue reading The problem with dynamic languages and large projects

Functional programming in PHP

With the introduction of lambda (or anonymous) functions in PHP 5.3 it is now possible to introduce some nice features from the functional programming paradigm.

Higher order function

When a method (or function) accepts another function as parameter this function is called a higher order function. The following already worked in PHP < 5.3 and is a simple implementation of functional programming with callable variables.

$data = array(10,2,6,4);
function cmp($a, $b) {
    if ($a == $b) return 0;
    return $a < $b ? -1 : 1;
usort($data, "cmp");

Continue reading Functional programming in PHP

Finally PHP 5.5 is here

Yes, the pun is intended. With the release of the first PHP5.5 alpha support for the finally keyword is now supported. Earlier I wrote that I think PHP has a lot of catching up to do compared with other languages like Python, Java and C#. Now it looks like that is going to happen with the new coming release of PHP 5.4.

Finally keyword

There is not much to explain ๐Ÿ™‚ Everyone who has experience in Java or other bug OO languages already know what is does.

try {
    // Do something
} catch(Exception $e) { 
    // Catch the exception that happens
} finally { 
    // Always execute after handling the exception or when no exception has occured

Continue reading Finally PHP 5.5 is here

Advanced example of PHP traits

With the introduction of PHP 5.4 traits are included. A trait is a mix between an interface and a class. Look onย wikipediaย for a more detail description on what traits are. Or check Scala which also uses traits.

When searching on the web for a real world example of using traits I did not find very much which was directly clear and somewhat more complete then FooBar or HelloWorld. So I tried to make a more real life example with animals and give an example how to use traits in PHP 5.4. Continue reading Advanced example of PHP traits

Is PHP suffering from a handicap of a head start?

In dutch we have a nice expression (“Wet van de remmende voorsprong”) and translated in English it is something like The Law of the handicap of a head start. A company or a product can be very innovative but after a initial success it can be too slow to react on changes and then become outdated.

In my opinion PHP is suffering from this particular problem. PHP has made a real fast start and has an made it to a great deal of the websites of today. But now it is suffering from a burden of legacy and innovation is going really slow. To name a few:

  • With the release of PHP 5.3 we finally got namespaces, Java has it by design for a decade or so.
  • Annotations are used in a “poor mans version”, in comments instead of native in the language.
  • Dynamic typing, this is not really a PHP issue but it can work out very dangerous with SQL injections that are possible “by design”. A GET query parameter is not checked anyhow. All checking should be done by the programmer instead default in the language.
  • Refactoring, with the dynamic typing comes another problem that refactoring is almost impossible in large projects. We changing a function signature you must manually search for all references and change them. The only way to find out you forgot one is at runtime. The compiler will not complain.
  • Exception handling is not always used. Some newer classes are using exception throwing when something is going wrong. But a lot of older functions are not using exceptions but still generate a plain old E_ERROR or similar. This way it is impossible to catch some errors and react on it. And then people start using the @ sign.. with all consequences.

I think PHP should react on these legacy problems by supporting optional typing (like Scala does), so a programmer can make a choice and old legacy software will keep running and professional programmers can use static typing. And maybe drop all the old procedural functions or refactor them to support exceptions were possible. This way PHP can become better in my opinion. Because Java becomes very tempting for me ๐Ÿ™‚