PHP中的类对象循环

PHP中的类对象循环,php,Php,我试图循环遍历对象并获取\u dBrutto属性的值 object(oxPrice)#428 (6) { ["_roundPrices":"oxPrice":private]=> bool(true) ["_roundPrices":"ZmbEngine_oxPrice":private]=> bool(true) ["_dBrutto":protected]=> float(141.625) ["_dNetto":protected]=>

我试图循环遍历对象并获取
\u dBrutto
属性的值

object(oxPrice)#428 (6) {
  ["_roundPrices":"oxPrice":private]=>
  bool(true)
  ["_roundPrices":"ZmbEngine_oxPrice":private]=>
  bool(true)
  ["_dBrutto":protected]=>
  float(141.625)
  ["_dNetto":protected]=>
  string(7) "141.625"
  ["_dVat":protected]=>
  float(0)
  ["_blNetPriceMode":protected]=>
  bool(true)
}
我尝试了如下循环代码:

foreach($oxPrice as $oxPrc){
    var_dump($oxPrc);
}

但是没有成功

有什么建议或想法吗?
谢谢。

您可以不使用循环直接访问_dBrutto的值。 试试下面

var_dump(oxPrice->_dBrutto);

\u dBrutto
具有受保护类型,这意味着它只能由自身或扩展
oxPrice
类的类访问。如果可以修改这个类,可以创建一个getter或将属性公开(建议使用getter-public属性,因为滥用它的时机已经成熟)

然后您可以通过以下方式访问它:
$oxPrice->get_dBrutto()

如果您不能修改这个类,那么可能是有原因的。您可以强制它,但不建议这样做,我这样做只是为了测试,我不需要setter或getter方法,但需要测试增量、ID特定值或突变

因此,我们可以使用
ReflectionClass

$reflectionClass = new \ReflectionClass(oxPrice::class);
$reflectionProperty = $reflectionClass->getProperty('_dBrutto');
$reflectionProperty->setAccessible(true);
echo $reflectionProperty->getValue($oxPrice); // 141.625
下面是函数的用法示例:

var_dump(getProtectedProperty('_dBrutto', $oxPrice)); // 141.625

setProtectedProperty('_dBrutto', 150.666, $oxPrice);
var_dump(getProtectedProperty('_dBrutto', $oxPrice)); // 150.666

function setProtectedProperty($property, $value, $object)
{
    $reflectionClass = new \ReflectionClass(get_class($object));
    $reflectionProperty = $reflectionClass->getProperty($property);
    $reflectionProperty->setAccessible(true);
    $reflectionProperty->setValue($object, $value);
}

function getProtectedProperty($property, $object)
{
    $reflectionClass = new \ReflectionClass(get_class($object));
    $reflectionProperty = $reflectionClass->getProperty($property);
    $reflectionProperty->setAccessible(true);
    $reflectionProperty->getValue($object);
}

@这家伙你可以;不只是非公共属性…为什么不只是使用数组?从var_dump获得的输出是什么?没有foreach的输出。如果这是您的类,请添加一些get方法。如果不是,那么写它的人要么已经这样做了,要么一开始就不想让你访问它们。如果不是私有的或受保护的,则此解决方案将抛出
异常:无法访问受保护的属性…
我必须访问_dBrutto并根据情况将其乘以某个数字。这就是为什么我有一个大问题。主要问题不是获取它,因为有一个函数getBruttoPrice将返回_dBrutto,但我需要更改它。稍后,更改后的价格将导出到csv文件中…我想您可能需要更新您的问题-我相信这回答了您当前的问题,但对于更改它,由于没有setter方法,您可以使用相同的方法,但不是使用
$reflectionProperty->getValue($oxPrice)
,而是使用
$reflectionProperty->setValue($oxPrice,)您好,我尝试了
$reflectionClass=new\reflectionClass(get_class($oxPrice))
然后
$reflectionProperty=$reflectionClass->getProperty(“'u dBrutto')但当我执行
var_dump($reflectionProperty)时我收到:
object(ReflectionProperty)#942(2){[“name”]=>string(8)][u dBrutto”[“class”]=>string(7)“oxPrice”}
你已经成功了!您正在转储反射属性对象。您需要设置为可访问,然后获取或设置它<代码>$reflectionProperty->setAccessible(true)然后是$reflectionProperty->setValue($oxPrice,);。要检查新值,
var_dump($reflectionProperty->getValue($oxPrice))
@Dextranovich-在答案中添加了一些获取和设置的示例
$reflectionClass = new \ReflectionClass(oxPrice::class);
$reflectionProperty = $reflectionClass->getProperty('_dBrutto');
$reflectionProperty->setAccessible(true);
echo $reflectionProperty->getValue($oxPrice); // 141.625
var_dump(getProtectedProperty('_dBrutto', $oxPrice)); // 141.625

setProtectedProperty('_dBrutto', 150.666, $oxPrice);
var_dump(getProtectedProperty('_dBrutto', $oxPrice)); // 150.666

function setProtectedProperty($property, $value, $object)
{
    $reflectionClass = new \ReflectionClass(get_class($object));
    $reflectionProperty = $reflectionClass->getProperty($property);
    $reflectionProperty->setAccessible(true);
    $reflectionProperty->setValue($object, $value);
}

function getProtectedProperty($property, $object)
{
    $reflectionClass = new \ReflectionClass(get_class($object));
    $reflectionProperty = $reflectionClass->getProperty($property);
    $reflectionProperty->setAccessible(true);
    $reflectionProperty->getValue($object);
}