Created
October 12, 2015 22:15
-
-
Save carlbennett/fde4b1c2f7b348015d9b to your computer and use it in GitHub Desktop.
Tests serializing a class with custom serialize functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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