php 覆盖实例变量的 = 操作
可能的重复:
PHP 中的运算符重载
我正在尝试创建一个允许在 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”。可以这样做吗?
预先感谢,
问候, 鲍勃
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最接近的选项是
Spl 类型
图书馆。问题是它被认为是实验性的,可能会被弃用。好处是它完全符合您想要做的事情。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.