PHP code example of omega-mvc / serializable-closure
1. Go to this page and download the library: Download omega-mvc/serializable-closure library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
omega-mvc / serializable-closure example snippets
use Omega\SerializableClosure\SerializableClosure;
use Omega\SerializableClosure\UnsignedSerializableClosure;
// Create a closure
$closure = function (int $a, int $b): int {
return $a + $b;
};
// Create an unsigned serializable closure
$unsignedSerializable = new UnsignedSerializableClosure($closure);
// Serialize the closure
$serialized = serialize($unsignedSerializable);
// Unserialize the closure
$unserializedSerializable = unserialize($serialized);
// Invoke the unserialized closure
$result = $unserializedSerializable(5, 3); // $result will be 8
echo "Native Serialization Result: " . $result . PHP_EOL;
// Alternatively, using the main SerializableClosure class without a secret key
SerializableClosure::setSecretKey(null); // Ensure no secret key is set for native serialization
$serializableNative = new SerializableClosure($closure);
$serializedNative = serialize($serializableNative);
$unserializedNative = unserialize($serializedNative);
$resultNative = $unserializedNative(10, 7); // $resultNative will be 17
echo "Native Serialization (via SerializableClosure) Result: " . $resultNative . PHP_EOL;
use Omega\SerializableClosure\SerializableClosure;
use Omega\SerializableClosure\Exception\InvalidSignatureException;
use Omega\SerializableClosure\Exception\MissingSecretKeyException;
// Set a secret key for HMAC signing
$secretKey = 'your_super_secret_key';
SerializableClosure::setSecretKey($secretKey);
// Create a closure with bound variables
$multiplier = 2;
$closureWithBound = function (int $number) use ($multiplier): int {
return $number * $multiplier;
};
// Create a signed serializable closure
$signedSerializable = new SerializableClosure($closureWithBound);
// Serialize the closure
$serializedSigned = serialize($signedSerializable);
// --- Simulate receiving and unserializing the closure elsewhere ---
// At the receiving end, the secret key must be the same
SerializableClosure::setSecretKey($secretKey);
try {
$unserializedSignedSerializable = unserialize($serializedSigned);
$resultSigned = $unserializedSignedSerializable(10); // $resultSigned will be 20
echo "Signed Serialization Result: " . $resultSigned . PHP_EOL;
} catch (InvalidSignatureException $e) {
echo "Error: Invalid signature detected. The serialized closure may have been tampered with." . PHP_EOL;
} catch (MissingSecretKeyException $e) {
echo "Error: Secret key is missing for signature verification." . PHP_EOL;
}
// --- Example of signature verification failure ---
echo "\nSimulating tampered data:" . PHP_EOL;
$tamperedSerialized = $serializedSigned;
// Tamper with the data (e.g., change a character in the serialized string)
$tamperedSerialized = str_replace('use ($multiplier', 'use ($unrelatedVariable', $tamperedSerialized);
SerializableClosure::setSecretKey($secretKey); // Re-set key for verification
try {
$unserializedTampered = unserialize($tamperedSerialized);
$resultTampered = $unserializedTampered(10);
echo "Tampered Result (should not be reached): " . $resultTampered . PHP_EOL;
} catch (InvalidSignatureException $e) {
echo "Successfully caught invalid signature for tampered data: " . $e->getMessage() . PHP_EOL;
}
// --- Example of missing secret key ---
echo "\nSimulating missing secret key:" . PHP_EOL;
SerializableClosure::setSecretKey(null); // No secret key set
try {
unserialize($serializedSigned);
} catch (MissingSecretKeyException $e) {
echo "Successfully caught missing secret key error: " . $e->getMessage() . PHP_EOL;
}
sh
// File name: /etc/php/your_php_version/mods_available/xdebug.ini
zend_extension=xdebug.so
xdebug.show_exception_trace=0
xdebug.mode=coverage
zend_assertion=1
assert.exception=1
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.