如何检查对象属性类型


How do I check an objects property types?

对不起标题,我找不到更好的写法=/

我收到一个名为 ErrorBase 的错误对象。

如果只有一个错误,它将返回以下内容:

public 'ErrorBase' => 
      public 'CODIGO_ERRO' => string '1' (length=1)
      public 'MENSAGEM_ERRO' => string 'Autenticação Inválida' (length=24)
      public 'TIPO_ERRO' => string 'Usuario' (length=7)

但是,如果有多个错误,它将向我返回如下对象数组:

public 'ErrorBase' => 
array
  0 => 
    object(stdClass)[30]
      public 'CODIGO_ERRO' => string '1' (length=1)
      public 'MENSAGEM_ERRO' => string 'Autenticação Inválida' (length=24)
      public 'TIPO_ERRO' => string 'Usuario' (length=7)
  1 => 
    object(stdClass)[31]
      public 'CODIGO_ERRO' => string '002' (length=3)
      public 'MENSAGEM_ERRO' => string 'teste 002' (length=9)
      public 'TIPO_ERRO' => string 'tipo 002' (length=8)
  2 => 
    object(stdClass)[32]
      public 'CODIGO_ERRO' => string '003' (length=3)
      public 'MENSAGEM_ERRO' => string 'teste 003' (length=9)
      public 'TIPO_ERRO' => string 'tipo 003' (length=8)
  3 => 
    object(stdClass)[33]
      public 'CODIGO_ERRO' => string '004' (length=3)
      public 'MENSAGEM_ERRO' => string 'teste 004' (length=9)
      public 'TIPO_ERRO' => string 'tipo 004' (length=8)

我该如何处理这些情况?如何检查是否存在对象数组或只有一个对象?

提前感谢任何帮助。

试试...

is_object() 和 is_array()

>如果$variable包含数组,则is_array($variable)返回 true,否则返回 false。

使用 is_array()

if (is_array($this->ERROR_BASE))

测试对象的类:

if ($var instanceof ErrorBase) {

要测试它是否是一个数组:

if (is_array($var)) {

使用 gettype() 返回变量类型。

或使用is_array/is_object来测试每个