Php type casting

So I have been writing a PHP library for automating a laboratory management of equipment, experiments, results and grading… its pretty simple starting point version 0.1 based on an earlier php-mysql project I did. I had implemented data handling using arrays, but I am using PDO this time around and I have a chance to return data from select queries as standard objects. So I wanted to patch up things and convert arrays to standard objects and the other way around instead of rewriting everything in the existing part of the library. As it turns out, you can convert a standard object to an array by type-casting, and the other way around. Also, overloading isn’t possible in PHP as it is in strongly typed languages. This means that you cannot overload a class constructor, something I like doing in an effort to make the code more generic. As it turns out, the only way to have different instantiation of a class, through a list of arguments or through an array, for example, is to have optional arguments and combine that with conditional checks inside the constructor.


class Person{
	public $id;
	public $names;
	public $address;
	public $age;
	public $timestamp;
	
	function __construct($n, $add=NULL, $ag=NULL) {
		if(is_array($n)){
			$this->names = $n[0];
			$this->address = $n[1];
			$this->age = $n[2];
		}else {
			$this->names = $n;
			$this->address = $add;
			$this->age = $ag;
		}		
		$this->id = '';//to be autoincremented in database
		$this->timestamp = date("Y-m-d H:i:s", time());//=NOW()
	}
}

echo "<h2>Object to array:</h2>";
$jack = new Person("Jackson Ndegwa","Chiromo lane","31");
print_r($jack);
echo "<br>from object->names: $jack->names<br>";
$jack = (array)$jack;
print_r($jack);
$key = "names";
echo "<br>from array[\"names\"]: "; echo $jack[$key]."<br><h2>Array to Object</h2><br>";

$jack = array("names"=>"Jackson Ndegwa","address"=>"Chiromo lane","age"=>"31");
print_r($jack);
echo "<br>from array[\"names\"] : ".$jack["names"]."<br>";
$jack = (object)$jack;
print_r($jack);
echo "<br>from object->names: $jack->names<br>";
$jack = array("Jackson Ndegwa","Chiromo lane","31");
print_r($jack);
$jack = new Person($jack);
print_r($jack);

 

Disabling the touch-pad in Linux when using an external USB mouse on a laptop

Sometimes I use an external USB [wireless] mouse when doing things that are cumbersome to do with a touch-pad, like 3D modeling in CAD software [Sketchup]. At such times, I have to find a setting in the system and disable the touch-pad. It works well but, when I later login after the laptop goes to sleep or I unplug and replug the external mouse for whatever reason, those settings are forgotten. Its pretty easy to disable the touch-pad on the linux terminal:

synclient TouchpadOff=1

and re-enable:

synclient TouchpadOff=0

You can even create an alias to make the command shorter and easier to remember and type-in

sudo nano ~/.bash_aliases

and add the following line

alias touchpaddoff='synclient TouchpadOff=1'
alias touchpadon='synclient TouchpadOff=0'

or a toggle function
Continue reading “Disabling the touch-pad in Linux when using an external USB mouse on a laptop”