PHP 5.4 – Function Array Dereferencing


PHP now supports array dereferencing directly from a function call.
Prior to version 5.4 you had to store the returning value from a function into a variable, and then use the variable.
The most frequent uses will be when using ‘preg_match’ or ‘explode’ kind of functions, functions that return an array. So instead of the following:

$data  = "piece1 piece2 piece3 piece4";
$pieces = explode(" ", $data);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

We could instead shorten this to:

$data  = "piece1 piece2 piece3 piece4";
echo explode(" ", $data)[0];

Another example using the ‘getdate()’ function.

Read more »

PHP 5.4 – Traits, Closures, and Prototype-based Programming


According to Wikipedia, prototype-based programming is “a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is performed via a process of cloning existing objects that serve as prototypes”With magic methods, traits, and anonymous functions – this is now possible in PHP.

    class SimpleXML
    {
        use \Prototype;
    }

    $SimpleXML = new SimpleXML();
    $SimpleXML->load = function( $path )
    {
        if( file_exists( $path ))
            return simplexml_load_file( $path );

        return null;
    };

    $SimpleXML->load = function( $data )
    {
        return simplexml_load_string( $data );
    };

The previous code uses SimpleXML and contains a class declaration by that name with a single trait that is shown later. The class is then instantiated and 2 anonymous functions are added to the prototype. With the above code and the Prototype trait, the following statements are both possible.

    $doc = $SimpleXML->load( 'test.xml' );
    $doc = $SimpleXML->load( 'test' );

The first method triggers the Closure that uses simplexml_load_file and the second method triggers simplexml_load_string respectively. You may notice that both functions are assigned to the same property. Using the prototype trait, it is possible to define 2 functions with the same name and number of arguments. This is a lot like traditional method overloading which was previously not possible with PHP. Overloading in this way is primitive and requires that overloaded methods meet certain criteria, namely that a given method return null if the input is not valid.

Read more »

Linux – scp copy all hidden dot files


The scp command copies files between servers (computers) on a network. It uses ssh for data transfer, and uses the same authentication and provides the same security as ssh.

scp Command

The correct syntax is as follows to copy all files including hidden dot files:

$ scp -rp /path/to/source/. user@server2:/path/to/dest/

Where,

-r : Recursively copy entire directories. Note that scp follows symbolic links encountered in the tree trave
-p : Preserves modification times, access times, and modes from the original file.
/path/to/source/. : Appending a dot (.) symbol is very important when you specify /path/to/source as a source path. If you skip dot in path it will only copy normal files and scp will skip all hidden files.
rsync Command

I recommend that you use rsync command to copy files between Unix / Linux based servers and workstations.

$ rsync -avzP /path/to/source/ user@server2:/path/to/dest/

OR

$ rsync -avzP /path/to/source/ user@192.168.1.5:/path/to/dest/