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);