更改对象中的受保护值(PHP)

更改对象中的受保护值(PHP),php,object,visibility,Php,Object,Visibility,当我对数组$mailer执行var_转储时,我得到: object(Fooman_EmailAttachments_Model_Core_Email_Template_Mailer)#352 (8) { ["_emailInfos":protected]=> array(3) { [0]=> object(Mage_Core_Model_Email_Info)#409 (11) { ["_bccNames":protected]=> array(0) { } ["_bccEma

当我对数组
$mailer
执行var_转储时,我得到:

object(Fooman_EmailAttachments_Model_Core_Email_Template_Mailer)#352 (8) {
["_emailInfos":protected]=>
array(3) {
[0]=>
object(Mage_Core_Model_Email_Info)#409 (11) {
["_bccNames":protected]=>
array(0) {
}
["_bccEmails":protected]=>
array(0) {
}
["_toNames":protected]=>
array(1) {
[0]=>
string(13) "My Name"
}
["_toEmails":protected]=>
array(1) {
[0]=>
string(17) "justatest@test.com"
}
["_data":protected]=>
array(0) {
}
["_hasDataChanges":protected]=>
bool(false)
["_origData":protected]=>
NULL
["_idFieldName":protected]=>
NULL
["_isDeleted":protected]=>
bool(false)
["_oldFieldsMap":protected]=>
array(0) {
}
["_syncFieldsMap":protected]=>
array(0) {
}
}

我想编辑
\u-toEmails
,但如何访问和编辑它?

阅读类
Fooman\u-EmailAttachments\u-Model\u-Core\u-Email\u-Template\u-Mailer
的文档。应该有一个方法可以调用来编辑信息,

类似于
$mailer->setEmails('foo')
。如果没有,则不需要修改数据。

阅读类
Fooman\u EmailAttachments\u Model\u Core\u Email\u Template\u Mailer
的文档。应该有一个方法可以调用来编辑信息,

类似于
$mailer->setEmails('foo')
。如果没有,那么数据就不会被修改。

我不必告诉您,这可能是隐藏的原因,而且很可能有一种方法可以在不直接访问它的情况下设置它(其他人肯定会告诉您),但是您可以扩展该类并添加自己的方法来设置它:

<?php
    class Foo {
        protected $_destroyDatabase = false;
    }

    class Bar extends Foo {
        public function SetDestroyDatabase($destroyDatabase) {
            $this->_destroyDatabase = $destroyDatabase;
        }
    }

    $foo = new Foo();
    $foo->_destroyDatabase = true; //Fatal error
    var_dump($foo);
    /*
        object(Foo)#1 (1) {
          ["_destroyDatabase:protected"]=>
          bool(false)
        }
    */

    $bar = new Bar();
    $bar->SetDestroyDatabase(true); //Success
    var_dump($bar);
    /*
        object(Bar)#2 (1) {
          ["_destroyDatabase:protected"]=>
          bool(true)
        }
    */
?>


我不必告诉您这可能是隐藏的原因,而且很可能有一种方法可以在不直接访问它的情况下设置它(正如其他人肯定告诉您的那样),但是您可以扩展该类并添加自己的设置方法:

<?php
    class Foo {
        protected $_destroyDatabase = false;
    }

    class Bar extends Foo {
        public function SetDestroyDatabase($destroyDatabase) {
            $this->_destroyDatabase = $destroyDatabase;
        }
    }

    $foo = new Foo();
    $foo->_destroyDatabase = true; //Fatal error
    var_dump($foo);
    /*
        object(Foo)#1 (1) {
          ["_destroyDatabase:protected"]=>
          bool(false)
        }
    */

    $bar = new Bar();
    $bar->SetDestroyDatabase(true); //Success
    var_dump($bar);
    /*
        object(Bar)#2 (1) {
          ["_destroyDatabase:protected"]=>
          bool(true)
        }
    */
?>


下面是一个非常简单的示例(没有错误检查),说明如何使用
ReflectionClass

function setProtectedProperty($obj, $property, $value) {
  $reflection = new ReflectionClass($obj);
  $property = $reflection->getProperty($property);
  $property->setAccessible(true);
  return $property->setValue($obj, $value);
}

setProtectedProperty($mailer, '_toEmails', ['foo@bar.com']);

下面是一个非常简单的示例(没有错误检查),说明了如何使用
ReflectionClass

function setProtectedProperty($obj, $property, $value) {
  $reflection = new ReflectionClass($obj);
  $property = $reflection->getProperty($property);
  $property->setAccessible(true);
  return $property->setValue($obj, $value);
}

setProtectedProperty($mailer, '_toEmails', ['foo@bar.com']);

你不能直接这么做。我不知道您正在使用的库,但它可能有一个setter方法来更改此值,类似于
$object->setToEmails
它不是数组,而是对象。请向我们显示类
Fooman\u EmailAttachments\u Model\u Core\u Email\u Template\u Mailer的代码。可能此属性有一个setter方法。您不能直接执行此操作。我不知道您正在使用的库,但它可能有一个setter方法来更改此值,类似于
$object->setToEmails
它不是数组,而是对象。请向我们显示类的代码
Fooman\u EmailAttachments\u Model\u Core\u Email\u Template\u Mailer