Created
February 22, 2017 23:24
-
-
Save siscodev93/7be9c2be14e657009cc44910aa152661 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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