Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php覆盖实例变量的=操作_Php_Oop_Class_Overwrite_Type Safety - Fatal编程技术网

php覆盖实例变量的=操作

php覆盖实例变量的=操作,php,oop,class,overwrite,type-safety,Php,Oop,Class,Overwrite,Type Safety,可能重复: 我试图创建一个库,允许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\

可能重复:

我试图创建一个库,允许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;我想在这里调用uuu集或其他方法,而不是使用$test3。有可能吗

提前感谢,

您好,
Bob

最接近的选项是库。问题是它被认为是实验性的,可能会被弃用。它的好处在于,它完全符合你的要求。

所以,要得到我想要的东西,唯一的办法就是使用实验课?但是如果我做对了,这是可能的,因为Spl类型与我想要创建的类型相同。那他们是怎么做到的呢?有什么方法可以让我看到这个类的源代码吗?或者是用另一种语言php?仍然非常感谢你的回答,顺便说一句,他们用C语言编写了它,并将其作为扩展添加。我想,如果需要的话,您可以自己编写代码并维护它。