문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top