Skip to content

Instantly share code, notes, and snippets.

@visb
Last active December 30, 2015 04:08
Show Gist options
  • Select an option

  • Save visb/7773528 to your computer and use it in GitHub Desktop.

Select an option

Save visb/7773528 to your computer and use it in GitHub Desktop.
<?php
abstract class TypeObject
{
protected $type;
protected $value;
public function __construct($value)
{
$this->setType(strtolower(get_called_class()));
$this->setValue($value);
}
public function equal(TypeObject $object)
{
return $this->getType() == $object->getType() && $this->getValue() == $object->getValue();
}
public function setValue($value)
{
$this->value = $value;
return $this;
}
public function getValue()
{
return $this->value;
}
protected function setType($type)
{
$this->type = $type;
return $this;
}
public function getType()
{
return $this->type;
}
}
class Int extends TypeObject
{
public function setValue($value)
{
$value = (int) $value;
return parent::setValue($value);
}
}
function load_type($type) {
// carrega o arquivo de $type
}
function call_type($type, $value)
{
if (!class_exists($type)) {
load_type($type);
}
return (new $type($value));
}
$types = array('number', 'int', 'float', 'double', 'string');
foreach($types as $type) {
eval("function {$type}(\$value) {
return call_type(__FUNCTION__, \$value);
}");
}
if (Int(1)->equal(Int(1))) {
echo Int(1)->getType();
} else {
echo 'false';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment