函数文档

rest_is_object()

💡 云策文档标注

概述

rest_is_object() 函数用于判断给定值是否具有对象特性,常用于 WordPress REST API 中验证对象类型数据。它检查值是否为对象或可序列化为数组。

关键要点

  • 函数接受一个参数 $maybe_object,为必需值,用于评估是否为对象。
  • 返回布尔值:如果值具有对象特性则返回 true,否则返回 false。
  • 函数逻辑包括检查空字符串、stdClass 实例、JsonSerializable 实例,并最终使用 is_array() 判断。

代码示例

function rest_is_object( $maybe_object ) {
    if ( '' === $maybe_object ) {
        return true;
    }

    if ( $maybe_object instanceof stdClass ) {
        return true;
    }

    if ( $maybe_object instanceof JsonSerializable ) {
        $maybe_object = $maybe_object->jsonSerialize();
    }

    return is_array( $maybe_object );
}

📄 原文内容

Determines if a given value is object-like.

Parameters

$maybe_objectmixedrequired
The value being evaluated.

Return

bool True if object like, otherwise false.

Source

function rest_is_object( $maybe_object ) {
	if ( '' === $maybe_object ) {
		return true;
	}

	if ( $maybe_object instanceof stdClass ) {
		return true;
	}

	if ( $maybe_object instanceof JsonSerializable ) {
		$maybe_object = $maybe_object->jsonSerialize();
	}

	return is_array( $maybe_object );
}

Changelog

Version Description
5.5.0 Introduced.