在使用 PHP 的 DOM 类(DOMNode、DOMEElement 等)时,我注意到它们拥有真正的只读属性。例如,我可以读取 DOMNode 的 $nodeName 属性,但无法写入(如果我这样做,PHP 会引发致命错误)。
如何在 PHP 中创建自己的只读属性?
在使用 PHP 的 DOM 类(DOMNode、DOMEElement 等)时,我注意到它们拥有真正的只读属性。例如,我可以读取 DOMNode 的 $nodeName 属性,但无法写入(如果我这样做,PHP 会引发致命错误)。
如何在 PHP 中创建自己的只读属性?
你可以这样做:
class Example {
private $__readOnly = 'hello world';
function __get($name) {
if($name === 'readOnly')
return $this->__readOnly;
user_error("Invalid property: " . __CLASS__ . "->$name");
}
function __set($name, $value) {
user_error("Can't set property: " . __CLASS__ . "->$name");
}
}
仅在您真正需要时才使用它 - 它比正常的属性访问要慢。对于 PHP,最好采用只使用 setter 方法从外部更改属性的策略。
您只能在属性声明期间初始化只读属性一次。
class Test {
public readonly string $prop;
public function __construct(string $prop) {
$this->prop = $prop;
}
}
--
class Test {
public function __construct(
public readonly string $prop,
) {}
}
尝试修改只读属性将导致以下错误:
Error: Cannot modify readonly property Test::$prop
但是仅使用 __get() 公开的私有属性对于枚举对象成员的函数是不可见的——例如 json_encode()。
我经常使用 json_encode() 将 PHP 对象传递给 Javascript,因为这似乎是传递具有大量从数据库填充的数据的复杂结构的好方法。我必须在这些对象中使用公共属性,以便将这些数据填充到使用它的 Javascript 中,但这意味着这些属性必须是公共的(因此冒着另一个程序员不在同一波长上的风险(或者可能我自己在一个糟糕的夜晚之后)可能会直接修改它们)。如果我将它们设为私有并使用 __get() 和 __set(),则 json_encode() 看不到它们。
有一个“只读”可访问性关键字不是很好吗?
我看到您已经得到了答案,但对于仍在寻找的人:
只需将所有“只读”变量声明为私有或受保护,并使用魔术方法 __get() 如下所示:
/**
* This is used to fetch readonly variables, you can not read the registry
* instance reference through here.
*
* @param string $var
* @return bool|string|array
*/
public function __get($var)
{
return ($var != "instance" && isset($this->$var)) ? $this->$var : false;
}
如您所见,我还保护了 $this->instance 变量,因为此方法将允许用户读取所有声明的变量。要阻止多个变量,请使用带有 in_array() 的数组。
这是一种从外部渲染类的所有属性 read_only 的方法,继承的类具有写访问权限;-)。
class Test {
protected $foo;
protected $bar;
public function __construct($foo, $bar) {
$this->foo = $foo;
$this->bar = $bar;
}
/**
* All property accessible from outside but readonly
* if property does not exist return null
*
* @param string $name
*
* @return mixed|null
*/
public function __get ($name) {
return $this->$name ?? null;
}
/**
* __set trap, property not writeable
*
* @param string $name
* @param mixed $value
*
* @return mixed
*/
function __set ($name, $value) {
return $value;
}
}
在php7中测试
对于那些寻找公开私有/受保护属性以进行序列化的方法的人,如果您选择使用 getter 方法将它们设为只读,这里有一种方法(@Matt: 以 json 为例):
interface json_serialize {
public function json_encode( $asJson = true );
public function json_decode( $value );
}
class test implements json_serialize {
public $obj = null;
protected $num = 123;
protected $string = 'string';
protected $vars = array( 'array', 'array' );
// getter
public function __get( $name ) {
return( $this->$name );
}
// json_decode
public function json_encode( $asJson = true ) {
$result = array();
foreach( $this as $key => $value )
if( is_object( $value ) ) {
if( $value instanceof json_serialize )
$result[$key] = $value->json_encode( false );
else
trigger_error( 'Object not encoded: ' . get_class( $this ).'::'.$key, E_USER_WARNING );
} else
$result[$key] = $value;
return( $asJson ? json_encode( $result ) : $result );
}
// json_encode
public function json_decode( $value ) {
$json = json_decode( $value, true );
foreach( $json as $key => $value ) {
// recursively loop through each variable reset them
}
}
}
$test = new test();
$test->obj = new test();
echo $test->string;
echo $test->json_encode();
Class PropertyExample {
private $m_value;
public function Value() {
$args = func_get_args();
return $this->getSet($this->m_value, $args);
}
protected function _getSet(&$property, $args){
switch (sizeOf($args)){
case 0:
return $property;
case 1:
$property = $args[0];
break;
default:
$backtrace = debug_backtrace();
throw new Exception($backtrace[2]['function'] . ' accepts either 0 or 1 parameters');
}
}
}
这就是我处理获取/设置我的属性的方式,如果你想让 Value() 只读......那么你只需让它执行以下操作:
return $this->m_value;
现在的函数 Value() 要么获取要么设置。