Skip to content

Instantly share code, notes, and snippets.

@carlbennett
Created October 12, 2015 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlbennett/fde4b1c2f7b348015d9b to your computer and use it in GitHub Desktop.
Save carlbennett/fde4b1c2f7b348015d9b to your computer and use it in GitHub Desktop.
Tests serializing a class with custom serialize functions
#!/usr/bin/php
<?php
class Test implements Serializable {
public function __tostring() {
return "asdf";
}
public function serialize() {
return "qwerty";
}
public function unserialize($data) {
echo $data;
}
}
$test = new Test();
$a = (string) $test; // serialize via type-casting
$b = $test->serialize(); // serialize via Serializable's serialize()
$c = $test->__tostring(); // serialize via __tostring()
$d = serialize($test); // serialize via global php namespace's serialize()
echo "a = " . $a . "\n"; // print $a to stdout
echo "b = " . $b . "\n"; // print $b to stdout
echo "c = " . $c . "\n"; // print $c to stdout
echo "d = " . $d . "\n"; // print $d to stdout
// results on php-cli 5.5.27:
// a = asdf
// b = qwerty
// c = asdf
// d = C:4:"Test":6:{qwerty}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment