我正在尝试创建一个方法,该方法允许我使用setVal()函数在类内设置属性,如果用户试图在不使用"forceSet"函数的情况下从类外设置值,那么它将引发异常。
问题是,即使$forceSet为true,它也会抛出异常。如果我手动将类中的属性设置为具有私有访问权限,那么一切都可以正常工作,但这不是一个选项,因为我希望能够动态设置类中的各种属性。
class test
{
private $_allowedCols = array('title', 'name', 'surname');
public function __set($n,$v)
{
$this->setVal($n, $v);
}
public function setVal($name, $value, $forceSet=false)
{
if (!$forceSet && !in_array($this->_allowedCols, $name))
{
throw new Exception('cant set value');
}
$this->$name = $value;
}
}
$b = new test;
$b->setVal('blah', 'test', true);
print_r($b);
exit;
我想做的是将$_POST中的所有值设置为对象中的属性。我想检查$_allowedCols,以确保只有我想要的值被放入对象中,但有时我可能想强制从不在$_allowed Cols中的代码中输入值。
有什么想法吗?
这些技巧会奏效,但使用内部数组可能会更干净。类似于:
class test
{
private $data = array();
public function __set($n,$v)
{
if (isset($this->data[$n])) return $this->data[$n] = $v;
throw new Exception('cant set value');
}
public function __get($n)
{
if (isset($this->data[$n])) return $this->data[$n];
throw new Exception('cant retrieve value');
}
public function setVal($name, $value)
{
$this->data[$name] = $value;
}
}
但如果你想坚持你的方法,那么:
class test
{
private $forceFlag = false;
public function __set($name,$value)
{
if ($this->forceFlag) return $this->$name = $value;
throw new Exception('cant set value');
}
public function setVal($name, $value)
{
$this->forceFlag = true;
$this->$name = $value;
$this->forceFlag = false;
}
}
如果查看异常的堆栈跟踪,您会注意到设置__set
的调用是由以下行触发的:
$this->$name = $value;
然后在__set
中,它执行$this->setVal($n, $v)
,后者使用默认值false,从而抛出异常。要解决此问题,您可以将__set
中的呼叫修改为:
$this->setVal($n, $v, true);
有了上面的代码,这一行:
$this->$name = $value;
调用:
test::__set('blah', 'test');
因为test::$blah
是未定义的,它反过来调用:
test::setVal('blah', 'test', false);
一个可能但并不完美的解决方法是:
public function setVal($name, $value, $forceSet=false)
{
if (!$forceSet && isset($value))
{
throw new Exception('cant set value');
}
$this->$name = null;
$this->$name = $value;
}
虽然我不确定你的代码的意义。
看起来你为PHP提供的开箱即用的功能编写了很多代码:
$b = new test;
$b->blah = 'test';
print_r($b);
您不需要__set
,也不需要使用setVal
(ue)函数。
但是,当您想要控制访问时,需要确保没有将其绑定到成员。相反,将其作为私人会员存储在地图中:
class test
{
private $values;
public function __set($n,$v)
{
$this->setVal($n, $v);
}
public function setVal($name, $value, $forceSet=false)
{
if (!$forceSet)
{
throw new Exception('cant set value');
}
$this->values[$name] = $value;
}
}
这样可以确保存在已设置的成员,从而不会再次触发__set
。
在测试了这么多选项之后。。是最适合我的
我选择这个是因为
- Exception的使用会终止整个脚本,或者在任何时候声明值时都必须捕获异常
- 通过扩展类可以很容易地覆盖
__set
和__get
- 可与多个类一起使用的实现
- 什么可以直接使用Object而不必添加另一个getter方法
- 锁定可能导致冲突
- 脚本不会更改现有的应用程序结构
- 可与Singleton一起使用
代码:
abstract class Hashtable
{
final $hashTable = array() ;
final function __set($n,$v)
{
return false ;
}
final function __get($n)
{
return @$this->hashTable[$n] ;
}
final function _set($n, $v)
{
$this->hashTable[$n] = $v ;
}
}
class Test extends Hashtable {} ;
$b = new Test();
$b->_set("bar","foo",true);
$b->_set("hello","world",true);
//$b->setVal("very","bad"); // false
$b->bar = "fail" ;
var_dump($b,$b->bar);
输出
object(Test)[1]
public 'hashTable' =>
array
'bar' => string 'foo' (length=3)
'hello' => string 'world' (length=5)
string 'foo' (length=3)
我希望这能帮助
感谢
:)