开发者

php overwrite the = action of an instanced variable [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Operator Overloading in PHP

I am trying to create a library that allows type-safe variables in php. I want to do the following:

开发者_如何学编程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"; 

Where $test = "3"; I want to call __set or a other method here instead of making $test "3". Is it possible to do that ?

Thanks in advance,

Greetings, Bob


Your closest option is the Spl Types library. The problem is that it is considered experimental and it may be deprecated. The boon is that it does exactly what you are trying to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜