You are looking at historical revision 4796 of this page. It may differ significantly from its current revision.

Introduction

This egg provides serialization/unserialization of objects in a format compatible with the popular PHP scripting language. This can be useful for communicating with PHP applications (such as Drupal or WordPress) over a socket connection, or for reading and writing serialized PHP application data in files or DBMS columns.

Examples

Serializing to PHP format

TODO


#;1> (use php-s11n)

#;2> (php-serialize #t)
"b:1;"

#;3> (php-serialize 3.1415)
"d:3.1415;"

#;4> (php-serialize '#("a" "b" "c"))
"a:3:{i:0;s:1:\"a\";i:1;s:1:\"b\";i:2;s:1:\"c\";}"

#;5> (php-serialize '((first_name . "Random") (last_name . "Hacker")))
"a:2:{s:10:\"first_name\";s:6:\"Random\";s:9:\"last_name\";s:6:\"Hacker\";}"

#;6> (php-serialize (void))
"N;"

Unserializing from PHP format

TODO


#;1> (use php-s11n)

#;2> (php-unserialize "a:3:{i:1;s:1:\"a\";i:2;s:1:\"b\";i:3;s:1:\"c\";}")
((1 . "a") (2 . "b") (3 . "c"))

#;3> (php-unserialize (php-serialize '((a . orange) (b . banana) (c . apple))))
(("a" . "orange") ("b" . "banana") ("c" . "apple"))

#;4> (php-unserialize (php-serialize '#(1 2 3 5 8 13 21 34)))
#(1 2 3 5 8 13 21 34)

Mapping PHP associative arrays to Scheme key-value lists

Because the interpretation of PHP's associative arrays can be ambigious and application-dependent, the php-s11n egg provides many parameters for hooking into and extending the serialization/unserialization process.

As an example of how to use this, say that you wanted to represent associative arrays using key-value lists as provided by the kvlists egg. First, you would define a custom writer and reader as follows:


(use php-s11n kvlists)

(define (php-s11n-write-array/kvlist value port)
  (if (kvlist? value)
      (php-s11n-write (kvlist->alist value))
      (error 'php-s11n-write "unable to serialize object" value)))

(define (php-s11n-read-array/kvlist port)
  (alist->kvlist (map (lambda (pair)
                        (cons (string->symbol (->string (car pair)))
                              (cdr pair)))
                      (php-s11n-read-array/alist port))))

(php-s11n-writer php-s11n-write-array/kvlist)
(php-s11n-array-reader php-s11n-read-array/kvlist)

Now, after loading the above code, you can test it out in the interpreter:


#;1> (php-unserialize (php-serialize '(a: orange b: banana c: apple)))
(a: "orange" b: "banana" c: "apple")

Author

Arto Bendiken

Requires

Serialization and unserialization

PHP's native data types are rather limited, meaning that only basic Scheme values can be serialized into and from this format. Scheme values are mapped to PHP format as follows:

Scheme PHP
void NULL
boolean boolean
integer integer (32-bit signed)
flonum float (64-bit)
string string
character string
symbol string
vector array (numerically indexed)
alist array (associative)
hash-table array (associative)
N/A object

php-serialize

[procedure] (php-serialize VALUE)

Returns a serialized string representation of VALUE.

php-unserialize

[procedure] (php-unserialize STRING)

Converts the serialized STRING into a value.

Parameters

php-s11n-writer

[procedure] (php-s11n-writer [PROC])

php-s11n-reader

[procedure] (php-s11n-reader [PROC])

php-s11n-array-reader

[procedure] (php-s11n-array-reader [PROC])

php-s11n-object-reader

[procedure] (php-s11n-object-reader [PROC])

Output

php-s11n-write

[procedure] (php-s11n-write VALUE [PORT])

Input

php-s11n-read

[procedure] (php-s11n-read [PORT])

php-s11n-read-null

[procedure] (php-s11n-read-null PORT)

php-s11n-read-boolean

[procedure] (php-s11n-read-boolean PORT)

php-s11n-read-integer

[procedure] (php-s11n-read-integer PORT)

php-s11n-read-float

[procedure] (php-s11n-read-float PORT)

php-s11n-read-string

[procedure] (php-s11n-read-string PORT)

php-s11n-read-array

[procedure] (php-s11n-read-array PORT)

php-s11n-read-array/alist

[procedure] (php-s11n-read-array/alist PORT)

php-s11n-read-array/hash-table

[procedure] (php-s11n-read-array/hash-table PORT)

php-s11n-read-object

[procedure] (php-s11n-read-object PORT)

License

 Copyright (c) 2006-2007 Arto Bendiken.
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to
 deal in the Software without restriction, including without limitation the
 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 sell copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 IN THE SOFTWARE.

Version history

Not released yed, but available from SVN.