【发布时间】:2011-11-11 12:58:33
【问题描述】:
我正在尝试创建一个允许在 php 中使用类型安全变量的库。我想做以下事情:
class int extends typeClass{
protected $value;
public function __construct($value){
// Check if the parameter is a integer.
if( ! is_int($value) ){
throw new typeIntegerException('This isn\t an Integer.');
}
$this->value = $value;
}
// Returns the $value instead of the int class
public function __get(){
return $this->value;
}
// Sets $value instead of the class
public function __set($value){
if( ! is_int($value) ){
throw new typeIntegerException('This isn\t an Integer.');
}
$this->value = $value;
}
}
$test = new int(5);
$test = "3";
其中 $test = "3";我想在这里调用 __set 或其他方法,而不是让 $test "3"。有可能吗?
提前致谢,
您好, 鲍勃
【问题讨论】:
标签: php oop class overwrite type-safety