# php-serialize

> PHP serialize/unserialize in Javascript

Latest version **5.1.3** (published 2025-03-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install php-serialize
pnpm add php-serialize
yarn add php-serialize
bun add php-serialize
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 5.1.3 |
| Published | 2025-03-20 |
| First published | 2015-06-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 8 |
| Dependencies | 0 |
| Unpacked size | 36.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 120 |
| Author | steelbrain |
| Maintainers | steelbrain |

## Links

- npm: https://www.npmjs.com/package/php-serialize
- Repository: https://github.com/steelbrain/php-serialize
- Homepage: https://php-serialize.com
- Issues: https://github.com/steelbrain/php-serialize/issues
- npm.io page: https://npm.io/package/php-serialize

## Recent versions

- 5.1.3 (latest) — 2025-03-20
- 5.1.2 — 2025-03-01
- 5.1.1 — 2025-03-01
- 5.1.0 — 2025-02-11
- 5.0.1 — 2024-07-15
- 5.0.0 — 2024-07-10
- 4.1.1 — 2023-05-10
- 4.1.0 — 2023-04-30
- 4.0.2 — 2020-04-20
- 4.0.1 — 2020-04-20
- 4.0.0 — 2020-04-19
- 3.0.1 — 2019-10-14
- 3.0.0 — 2019-04-20
- 2.1.0 — 2018-12-30
- 2.0.1 — 2018-08-17
- … 13 more at https://npm.io/package/php-serialize/versions

## README

# PHP-Serialize

It also supports `Serializable` objects decode. Here's how you can use them.

#### Installation

```sh
$ npm install php-serialize # If you're using npm
$ yarn add php-serialize # If you're using Yarn
```

#### Usage

```js
import {serialize, unserialize} from 'php-serialize'

class User {
  constructor({ name, age }) {
    this.name = name
    this.age = age
  }
  serialize() {
    return JSON.stringify({ name: this.name, age: this.age })
  }
  unserialize(rawData) {
    const { name, age } = JSON.parse(rawData)
    this.name = name
    this.age = age
  }
}
const steel = new User({ name: 'Steel Brain', age: 17 })
const serialized = serialize(steel)
const unserialized = unserialize(serialized, { User: User }) // Passing available classes
console.log(unserialized instanceof User) // true

const serializedForNamespace = serialize(steel, {
  'MyApp\\User': User,
})
// ^ Above code will serialize User class to given name
```

#### API

```js
export function serialize(
  item: any,
  phpToJsScope: Object = {},
  options: { encoding: 'utf8' | 'binary' } = { encoding: 'utf8' }
): string
export function unserialize(
  item: string,
  scope: Object = {},
  options: { strict: boolean, encoding: 'utf8' | 'binary' } = { strict: false, encoding: 'utf8' }
): any
export function isSerialized(
  item: any,
  strict: false
): boolean
```

#### License

This project is licensed under the terms of MIT License. See the License file for more info.

---
_Source: https://npm.io/package/php-serialize · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
