Nasrul Hazim Bin Mohamad

UI – Bootstrap

Jan
30

An easy way to setup a nice & clean UI, and responsive UI.

Bootsrap use HTML5 & CSS3. Please ensure you have the following:

<!DOCTYPE html>
<html lang="en">
  ...
</html>

Include the following in head

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.min.css" rel="stylesheet">

Include the following at the bottom – before end of body tag – to make sure the page load fast.

<!-- I use jQuery 1.9.0 -->
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>

Here is the screen shot of my test. 🙂

Bootstrap Test

Bootstrap Test

PHP – Share Session Between Server

Jan
28

Basically the process is to convert the Session of Server 1, as Cookies. Then in Server 2, we convert back to Session.

Just include below class in both server. Use SessionShare::encode('sessionpass','current_domain'); in Server 1 then redirect to Server 2(use header('Location: http://server2.com')). Then in Server 2, include the same class, decode the Cookies that we created to Session by calling SessionShare::decode('sessionpass');. You should be able to get the same Session values, shared between two servers. 🙂

class SessionShare {
	public static function encode($sessionpass,$domain) {
		
		if(empty($sessionpass) || empty($domain))
			return false;
			
		$op = base64_encode(serialize($_SESSION));
		
		setcookie($sessionpass, $op, false, '/', $domain);
	}
	
	public static function decode($sessionpass) {
		if(empty($sessionpass) || empty($domain))
			return false;
		
		session_start();
		
		$_SESSION = unserialize(base64_decode(@$_COOKIE[$sessionpass]));
	}
}

Enterprise Project development Phases (Infrastructure projects)

Jan
28

MySql and Deathly Swallows

Jan
28

mysql super user creation steps.

Jan
28

jqIpLocation jQuery IP To Location Plugin

Jan
28

PHP – Bytes Converter

Jan
28

A small function to convert bytes to B, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB.

class BytesToString {
	public static function toString($value) {
		$suffix = array('B','KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB');
		
		$level = 0;
		
		while($value > 1024)
		{
			$value /= 1024;
			$level++;
		}
		
		return round($value,1) . ' ' . $suffix[$level];
	}
}

echo '<p>4200 bytes = ' . BytesToString::toString(4200) . '</p>';
echo '<p>4200000 bytes = ' . BytesToString::toString(4200000) . '</p>';
echo '<p>420000000 bytes = ' . BytesToString::toString(420000000) . '</p>';
echo '<p>42000000000 bytes = ' . BytesToString::toString(42000000000) . '</p>';
echo '<p>4200000000000 bytes = ' . BytesToString::toString(4200000000000) . '</p>';

// 4200 bytes = 4.1 KiB
// 4200000 bytes = 4 MiB
// 420000000 bytes = 400.5 MiB
// 42000000000 bytes = 39.1 GiB
// 4200000000000 bytes = 3.8 TiB

PHP – Overloading (Object, Variable, Execution, Methods)

Jan
26

Overloading in PHP, basically to dynamically create properties and methods

I will put some samples on Overloading in Object, Variables, Execution and Methods. There’s few magic method required in Overloading; __set(), __get(), __isset(), __unset().

Overloading in Object

class Storage {
	private $_data = array();

	public function __set($name, $value) {
		$this->_data[$name] = $value;
	}

	public function __get($name) {
		if(isset($this->_data[$name])) { return $this->_data[$name]; }
	}

	public function __isset($name) {
		return isset($this->_data[$name]);
	}

	public function __unset($name) {
		unset($this->_data[$name]);
	}
}

$storage = new Storage();
$storage->label = 'new storage';

echo 'Storage label is `' . $storage->label . '`.';
unset($storage->label);
echo 'Storage label is ' . (isset($storage->label) ? 'set' : 'not set')  . '.';

// Output:
// Storage label is `new storage`.
// Storage label is not set.

Overloading in Variable

class MultiStorage {
	private $_text = array();
	private $_number = array();

	public function __set($name, $value) {

		if(is_int($value) || is_float($value)) {
			$this->_number[$name] = $value;
		} else if(is_string($value)) {
			$this->_text[$name] = $value;
		}
	}

	public function value($name, $type) {
		$t = '';

		if($type == 'number')
			$t = '_number';
		else if($type == 'text')
			$t = '_text';

		return $this->{$t}[$name];
	}

	public function __isset($name) {
		return (isset($this->_text[$name]) || isset($this->_number[$name]));
	}

	public function __unset($name) {
		unset($this->_text[$name]);
		unset($this->_number[$name]);
	}
}

$multi_storage = new MultiStorage();
$multi_storage->val = 'new multi storage';
$multi_storage->val = 1;

echo 'Multi Storage id is `' . $multi_storage->value('val','number') . '`.';
echo 'Multi Storage label is `' . $multi_storage->value('val','text') . '`.';
unset($multi_storage->val);
echo 'Storage id & label is ' . (isset($multi_storage->val) ? 'set' : 'not set')  . '.';

// Multi Storage id is `1`.
// Multi Storage label is `new multi storage`.
// Storage id & label is not set.

Overloading in Execution

class Formatter {
	private $_data = array();
	
	public function __set($name, $value) {
	
		if(is_int($value)) {
			$new_value = number_format($value,0);
		} else if(is_float($value)) {
			$new_value = number_format($value,2);
		} else if(is_bool($value)) {
			$new_value = $value ? true : false;
		} else {
			$new_value = $value;
		}
	
		$this->_data[$name] = $new_value;
	}
	
	public function __get($name) {
		if(isset($this->_data[$name])) { return $this->_data[$name]; }
	}
	
	public function __isset($name) {
		return isset($this->_data[$name]);
	}
	
	public function __unset($name) {
		unset($this->_data[$name]);
	}
}

$formatter = new Formatter();
$formatter->name = 'Abdullah';
$formatter->age = 25;
$formatter->money = 1324.232;
$formatter->employed = true;

echo '<p>Formatter::name -> ' . $formatter->name . '.</p>';
echo '<p>Formatter::age -> ' . $formatter->age . '.</p>';
echo '<p>Formatter::money -> ' . $formatter->money . '.</p>';
echo '<p>Formatter::employed -> ' . $formatter->employed . '.</p>';

// Formatter::name -> Abdullah.
// Formatter::age -> 25.
// Formatter::money -> 1,324.23.
// Formatter::employed -> 1.

Overloading in Methods

There’s differences in Overloading Methods, because we using __call() instead of __set(), __get(), __isset() and __unset() but the concept is similar.

class Math {
	public function __call($name, $args) {
		$ret = null;
		
		if($name == 'sum')
		{
			$ret = 0;
			if(count($args) > 0) {
				foreach($args as $k => $v)
				{
					if(is_int($v))
					{
						$ret += $v;
					}
				}
			}
		}
		else if($name == 'concat')
		{
			$ret = implode(';',$args);
		}
		else
		{
			trigger_error('Invalid method access', E_USER_ERROR);
		}
		
		return $ret;
	}
}

$math = new Math();

echo '<h4>Overloading in Methods</h4>';
echo '<p>Summation of 1,2,3,4,5,6 is ' . $math->sum(1,2,3,4,5,6) . '</p>';
echo '<p>Concat of 1,2,3,4,5,6 is ' . $math->concat(1,2,3,4,5,6) . '</p>';
echo '<p>Error of 1,2,3,4,5,6 is ' . $math->blabla(1,2,3,4,5,6) . '</p><hr>';

// Overloading in Methods
// Summation of 1,2,3,4,5,6 is 21
// Concat of 1,2,3,4,5,6 is 1;2;3;4;5;6
Overloading - Method Error

Overloading – Method Error

Hope all these Overloading types will give some ideas, on how to minimizing number of line of codes, reusability of codes, and helps to organize our Objects, Variables, Executions and Methods in proper way. Personally, all these overload types give me more flexibility of design the architecture of the application and how I’m going to write codes in proper manners, and more understandable work flow.

PHP – __autoload() & SPL

Jan
26

__autoload() called anytime a new object attempts to be instantiated

As a programmer, at least we need to include certain classes or files before we execute files. __autoload() will be a good place to include all required classes or files.

But we only able to define only one __autoload() and this may cause problem to people writing third-party libraries or codes since they might have already define this __autoload().

Use spl_autoload_register() to add multiple autoloading into SPL(Standard PHP Library)

function autoload_one()
{
	@require_once "CD.php";
	echo '<p>CD Class Loaded.</p>';
}

spl_autoload_register('autoload_one');

function autoload_two()
{
	@require_once "ARTIST.php";
	echo '<p>ARTIST Class Loaded.</p>';
}

spl_autoload_register('autoload_two');

$mydisk = new CD();
$myartist = new ARTIST();

var_dump(array($mydisk,$myartist));

Using @ before require_once to prevent errors to be thrown when one function tries to load the file but fails

One last thing about __autoload() and SPL, what if __autoload has been declared? So, here the solution.

function __autoload($classname)
{
	@require_once "CD.php";
}

function autoload_two()
{
	@require_once "ARTIST.php";
	echo '<p>ARTIST Class Loaded.</p>';
}

if(function_exists('__autoload'))
{
	spl_autoload_register('__autoload');
	echo '<p>CD Class Loaded.</p>';
}

spl_autoload_register('autoload_two');

$mydisk = new CD();
$myartist = new ARTIST();

var_dump(array($mydisk,$myartist));

p/s: I chose the last method to use __autoload() and SPL together, much clean & convenient.

PHP – Comparison(==) VS Identity(===) Operator

Jan
26

$isequal = ($obj1 == $obj2);

Comparison Operator(==), checks whether the two objects are the same class and whether all class variables have the same value.

$issameinstace = ($obj1 === $obj2);

Identity Operator(===), return true if both object variables refer to the same instance of an object.