Category Archives: Software

PHP 5.4 – Code reuse using Traits

Traits enables a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. PHP being a single inheritance language, traits enables horizontal composition of behavior – the application of class members without requiring inheritance. I’ll cover Traits in a separate post itself.

Below is a artificial example from the PHP documentation.


<?php

trait Hello
{
 public function sayHello()
 {
 echo 'Hello ';
 }
}

trait World
{
 public function sayWorld()
 {
 echo 'World';
 }
}

class MyHelloWorld
{
 use Hello, World;
 public function sayExclamationMark()
 {
 echo '!';
 }
}

$o = new MyHelloWorld();
$o->sayHello();
$o->sayWorld();
$o->sayExclamationMark();

?>

Which will output:
Hello World!

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/

Linux – What is tmpfs ?

tmpfs is a file system that stores all the files in virtual memory. tmpfs doesn’t create any files on your hard drive. So if you unmount a tmpfs file system, all the files residing in it are lost for ever.

tmpfs is similar to ramfs and RAM disk but with a few additional features. tmpfs is able to grow or shrink its space to accommodate files,and it can use swap space to store unneeded data. ramfs and RAM disk doesn’t have this capability.

Use of tmpfs in Linux

Debian, Ubuntu, Fedora, openSUSE, and many other Linux distributions use tmpfs file system to store all their run time data.

For example, in Ubuntu, if I execute the following command, I find that /run is mounted on a tmpfs filesystem.


$ df -h | grep tmpfs
tmpfs 401M 880K 400M 1% /run

The /run directory and its sub-directories store run time data of Linux processes and any running applications that may want to store their data.

You can check the actual [RAM + Swap] use of a tmpfs using df and du commands
Since tmpfs lives completely in the page cache and on swap, all tmpfs pages currently in memory will show up as cached.

tmpfs in some form or other is currently implemented in Sun/Solaris, Linux, and BSDs.