Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save siscodev93/7be9c2be14e657009cc44910aa152661 to your computer and use it in GitHub Desktop.

Select an option

Save siscodev93/7be9c2be14e657009cc44910aa152661 to your computer and use it in GitHub Desktop.
<?php
/*
* A way to to type cast objects before php7
*/
class test{
private $string = array('typeof' => 'string', 'value' => null);
private $int = array('typeof' => 'integer', 'value' => null);
private $double = array('typeof' => 'double', 'value' => null);
private $testObj = array('typeof' => 'test', 'value' => null);
public function __get($name)
{
return $this->{$name}['value'];
}
public function __set($var, $val)
{
$cast = $this->{$var}['typeof'];
if( gettype($val) === $cast || (gettype($val) === 'object' && get_class($val) === $cast))
{
$this->{$var}['value'] = $val;
}
else
{
trigger_error('$'.$var . " must be of type ". $this->{$var}['typeof']);
}
}
}
$test = new test();
$test->string = "100";
$test->int = 100;
$test->double = 100.00;
$test->testObj = $test;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment