dkx/private-properties-accessor

此包已被废弃,不再维护。未建议替代包。

无需反射即可访问私有属性的访问器

0.1.0 2019-08-23 06:42 UTC

This package is auto-updated.

Last update: 2024-01-23 16:47:15 UTC


README

无需反射即可访问私有属性的访问器

基于 https://ocramius.github.io/blog/accessing-private-php-class-members-without-reflection/

安装

$ composer require dkx/private-properties-accessor

使用方法

<?php

declare(strict_types=1);

use DKX\PrivatePropertiesAccessor\PrivatePropertiesAccessor;

$accessor = new PrivatePropertiesAccessor($class);
$prop = & $accessor->getProperty('propFromClass');
$prop = 'hello world';

var_dump($class->propFromClass);    // output: hello world

获取所有属性

<?php

$props = $accessor->getProperties();

此方法使用 get_object_vars 函数,这意味着它将不会返回父类的私有属性。