简体   繁体   English

我的变量出了什么问题?

[英]What's wrong with my variable?

I can do a print_r of an employee object that I'm trying to pass into another method in another class and get the following: 我可以为一个雇员对象做一个print_r,尝试将其传递给另一个类中的另一个方法并获取以下信息:

emp Object ( [db:emp:private] => PDO Object ( ) [salesId:emp:private] => )

When I attempt to pass this object, I get an undefined variable error message. 当我尝试传递此对象时,收到未定义的变量错误消息。 What am I doing wrong? 我究竟做错了什么? Is there a PHP function that I can use to test this somehow? 是否可以使用PHP函数以某种方式对其进行测试? This object works great anywhere else. 该对象在其他任何地方都适用。

This error occurs when you try and access a variable that has not been previously defined. 当您尝试访问以前未定义的变量时,会发生此错误。

 $object = new stdClass();
 // This will give an undefined variable notice
 echo $object->my_value

To avoid this you need to assign something to it 为了避免这种情况,您需要为其分配一些内容

 $object = new stdClass();
 // Assign test to my_value, then echo it, will output: test
 $object->my_value = "test";
 echo $object->my_value;

You can check for the presence of a variable using isset() 您可以使用isset()检查变量是否存在

 $object = new stdClass();
 // Check if my_value is set, if it is echo it
 if(isset($object->my_value)) {
      echo $object->my_value;
 }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM