Automattic\WooCommerce\Utilities

CallbackUtil::get_callback_signaturepublic staticWC 10.5.0

Get a stable signature for a callback that can be used for hashing.

This method normalizes callbacks into consistent string representations, regardless of changes in dynamic properties in callback instances.

Method of the class: CallbackUtil{}

No Hooks.

Returns

String. Normalized callback signature.

Usage

$result = CallbackUtil::get_callback_signature( $callback ): string;
$callback(callable|mixed) (required)
A PHP callback.

Changelog

Since 10.5.0 Introduced.

CallbackUtil::get_callback_signature() code WC 10.8.1

public static function get_callback_signature( $callback ): string {
	if ( is_string( $callback ) ) {
		// Standalone function.
		return $callback;
	}

	if ( is_array( $callback ) && 2 === count( $callback ) ) {
		$target = $callback[0];
		$method = $callback[1];

		if ( ( is_object( $target ) || is_string( $target ) ) && is_string( $method ) ) {
			// Array callback (class method).
			$class = is_object( $target ) ? get_class( $target ) : $target;
			return "{$class}::{$method}";
		}
	}

	if ( $callback instanceof \Closure ) {
		// Closure.
		try {
			return self::get_closure_signature( $callback );
		} catch ( \Exception $e ) {
			return 'Closure@' . spl_object_hash( $callback );
		}
	}

	if ( is_object( $callback ) ) {
		// Invokable object.
		try {
			return self::get_invokable_signature( $callback );
		} catch ( \Exception $e ) {
			return get_class( $callback ) . '::__invoke';
		}
	}

	// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- Fallback for unknown callback types.
	return serialize( $callback );
}