PHP中的只读属性? [英] read-only properties in PHP?

查看:189
本文介绍了PHP中的只读属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以使PHP对象的只读属性?我有一个带有几个数组的对象.我想像平常一样访问它们

Is there a way to make a read-only property of an object in PHP? I have an object with a couple arrays in it. I want to access them as I normally would an array

echo $objObject->arrArray[0];

但是,我不希望在构造完这些数组后就可以对其进行写入.构造局部变量就像PITA:

But I don't want to be able to write to those arrays after they're constructed. It feels like a PITA to construct a local variable:

$arrArray = $objObject->getArray1();
echo $arrArray[0];

无论如何,尽管它使数组保持在原始对象中,但并不能阻止我重写本地数组变量.

And anyways, while it keeps the array in the object pristine, it doesn't prevent me from re-writing the local array variable.

推荐答案

好吧,问题是您想从哪里阻止写入?

Well, the question is where do you want to prevent writing from?

第一步是将数组设置为保护或私有数组,以防止从对象范围之外进行写入:

The first step is making the array protected or private to prevent writing from outside of the object scope:

protected $arrArray = array();

如果从数组的外部",GETTER可以使您满意.要么

If from "outside" of the array, a GETTER will do you fine. Either:

public function getArray() { return $this->arrArray; }

并像访问它一样

$array = $obj->getArray();

public function __get($name) {
    return isset($this->$name) ? $this->$name : null;
}

并像这样访问它:

$array = $obj->arrArray;

请注意,它们不返回引用.因此,您不能在对象范围之外更改原始数组.您可以更改数组本身...

Notice that they don't return references. So you cannot change the original array from outside the scope of the object. You can change the array itself...

如果您确实需要完全不可变的数组,则可以使用 ArrayAccess ...

If you really need a fully immutable array, you could use a Object using ArrayAccess...

或者,您可以简单地扩展 ArrayObject 并覆盖所有的写作方法:

Or, you could simply extend ArrayObject and overwrite all of the writing methods:

class ImmutableArrayObject extends ArrayObject {
    public function append($value) {
        throw new LogicException('Attempting to write to an immutable array');
    }
    public function exchangeArray($input) {
        throw new LogicException('Attempting to write to an immutable array');
    }
    public function offsetSet($index, $newval) {
        throw new LogicException('Attempting to write to an immutable array');
    }
    public function offsetUnset($index) {
        throw new LogicException('Attempting to write to an immutable array');
    }
}

然后,只需将$this->arrArray设置为对象的实例:

Then, simply make $this->arrArray an instance of the object:

public function __construct(array $input) {
    $this->arrArray = new ImmutableArrayObject($input);
}

它仍然支持大多数数组用法:

It still supports most array like usages:

count($this->arrArray);
echo $this->arrArray[0];
foreach ($this->arrArray as $key => $value) {}

但是,如果您尝试对其进行写操作,则会得到LogicException ...

But if you try to write to it, you'll get a LogicException...

哦,但是要意识到,如果您需要对其进行写操作,那么您所要做的(在对象内)就是:

Oh, but realize that if you need to write to it, all you need to do (within the object) is do:

$newArray = $this->arrArray->getArrayCopy();
//Edit array here
$this->arrArray = new ImmutableArrayObject($newArray);

这篇关于PHP中的只读属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆