简体   繁体   English

在非对象错误symfony2上调用成员函数

[英]Call to a member function on a non-object error symfony2

This is an odd one. 这是一个奇怪的。 I have this method, that is the callback for the FormEvents::POST_SET_DATA event in an EventSubscriber of a form in Symfony2: 我有此方法,即Symfony2中表单的EventSubscriber中FormEvents::POST_SET_DATA事件的回调:

public function preSetData(FormEvent $event)
    {
        $data = $event->getData();
        $form = $event->getForm();

        $resource_provider = $data->getResourceProvider();
        $type = $resource_provider->getType(); //PROBLEM HERE

        \Doctrine\Common\Util\Debug::dump(is_object($resource_provider));
}

It has more code but the error is firing in the line with the comment PROBLEM HERE . 它具有更多代码,但错误在注释PROBLEM HERE的一行中触发。

The issue is that the dump there prints bool(true) , so it is an object, and have the data inside, but trying to call getType() , that exist, fails with this error: 问题是那里的转储打印了bool(true) ,所以它是一个对象,并且内部有数据,但是尝试调用存在的getType()失败,并出现以下错误:

Error: Call to a member function getType() on a non-object

To add more confusion, if I dump $resource_provider->getType() I get the expected content dumped, but the error remains. 更令人困惑的是,如果我转储$resource_provider->getType() ,则会得到预期的内容,但错误仍然存​​在。

Does someone knows or has a clue on what can be happening here? 有人知道或有什么线索可以在这里发生吗?

EDIT 编辑

Can't believe it! 不敢相信! I found the answer, but doesn't makes sense at all! 我找到了答案,但根本没有任何意义! I had to wrap the problematic code with a conditional like this: 我不得不用如下条件包装有问题的代码:

if(is_a($resource_provider, 'My\Bundle\Path\Entity\ResourceProvider')){}

and it worked! 而且有效! If someone can tell me what is the sense of that, I will happily select his/her answer as correct. 如果有人可以告诉我这是什么意思,我会很乐意选择他/她的答案正确。

The problem is that $resource_provider is probably null as error says $resource_provider is not an object. 问题是$ resource_provider可能为null,因为错误表明$ resource_provider不是对象。

When printing the value of $resource_provider and skipping the script, it shows the right object of class 'My\\Bundle\\Path\\Entity\\ResourceProvider' : 当打印$ resource_provider的值并跳过脚本时,它将显示类'My \\ Bundle \\ Path \\ Entity \\ ResourceProvider'的正确对象:

echo get_class($resource_provider); exit; // Display My\Bundle\Path\Entity\ResourceProvider and skip the script

When adding a condition to test is the object is of the expected class, the script doesn't crash anymore : 当添加测试条件是对象属于预期类时,脚本不再崩溃:

if(is_a($resource_provider, 'My\Bundle\Path\Entity\ResourceProvider')){}

In addition, when skipping script when it's not an object, $resource_provider is null and the script skipped : 另外,如果在不是对象的情况下跳过脚本,则$ resource_provider为null,并且跳过了脚本:

if(is_a($resource_provider, 'My\Bundle\Path\Entity\ResourceProvider')){

} else {
   var_dump($resource_provider); exit;
}

$resource_provider is an object and null in the same "execution" of the script. $ resource_provider是一个对象,在脚本的同一“执行”中为null。

So we can deduce that the EventSubcriber is loaded more than one time, perhaps before the form binding and after form binding for example and the first time with the expected object, and a second (or more) time with no value, as null, perhaps because the $resource_provider is not filled when submitting form or something else. 因此,我们可以推断出EventSubcriber加载了多次,例如,在表单绑定之前和之后,第一次加载了预期的对象,第二次(或更多次)没有值加载,例如为null因为在提交表单或其他内容时$ resource_provider未被填充。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Symfony2日期表单类型:致命错误:在非对象上调用成员函数setLenient() - Symfony2 date form type: Fatal error: Call to a member function setLenient() on a non-object 错误:使用Symfony组件形式的ChoiceType调用非对象上的成员函数 - Error: Call to a member function on a non-object using ChoiceType of Symfony component form 在非对象中调用成员函数bind() - Call to a member function bind() on a non-object in 在非对象上调用成员函数getClientOriginalExtension(),即使'file'=> true - Call to a member function getClientOriginalExtension() on a non-object even with 'file' => true 在非对象 - 授权上调用成员函数allow() - Call to a member function allow() on a non-object - authorization 致命错误:在Form.php中的非对象上调用成员函数toOptionArray() - Fatal error: Call to a member function toOptionArray() on a non-object in Form.php Symfony2,Form,试图获取非对象的属性 - Symfony2, Form, Trying to get property of non-object in 我不断收到致命错误:使用sqli查询在非对象发布上调用成员函数bind_param() - I keep getting Fatal error: Call to a member function bind_param() on a non-object posting with sqli query Zend \\ Form:在Zend / Form / Fieldset.php中的非对象上调用成员函数insert() - Zend\Form: Call to a member function insert() on a non-object in Zend/Form/Fieldset.php 在第82行上,在profile.php中的非对象上调用成员函数query() - Call to a member function query() on a non-object in profile.php on line 82
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM