PHPackages                             10quality/php-class-to-javascript - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. 10quality/php-class-to-javascript

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

10quality/php-class-to-javascript
=================================

PHP trait that casts (transforms, converts) classes into javascript objects (string version).

v1.0.0(9y ago)0291MITPHPPHP &gt;=5.4

Since Feb 23Pushed 9y ago2 watchersCompare

[ Source](https://github.com/10quality/php-class-to-javascript)[ Packagist](https://packagist.org/packages/10quality/php-class-to-javascript)[ RSS](/packages/10quality-php-class-to-javascript/feed)WikiDiscussions v1.0 Synced 2d ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Class to Javascript (PHP trait)
===============================

[](#class-to-javascript-php-trait)

[![Latest Stable Version](https://camo.githubusercontent.com/6b9d7305cf314e75f561cb307e27dd26121e06a3c3a8ae1d789d0426e70bba28/68747470733a2f2f706f7365722e707567782e6f72672f31307175616c6974792f7068702d636c6173732d746f2d6a6176617363726970742f762f737461626c65)](https://packagist.org/packages/10quality/php-class-to-javascript)[![Total Downloads](https://camo.githubusercontent.com/06d30799ac09509625ea1f60f5206388f089d67e2cb84bf770e43d045e73a189/68747470733a2f2f706f7365722e707567782e6f72672f31307175616c6974792f7068702d636c6173732d746f2d6a6176617363726970742f646f776e6c6f616473)](https://packagist.org/packages/10quality/php-class-to-javascript)[![License](https://camo.githubusercontent.com/2a0e3f5b8546aa8b5b7037e50af81d50f8d98e5f3a0b49eb95f6005c653663d3/68747470733a2f2f706f7365722e707567782e6f72672f31307175616c6974792f7068702d636c6173732d746f2d6a6176617363726970742f6c6963656e7365)](https://packagist.org/packages/10quality/php-class-to-javascript)

PHP trait that casts (transforms, converts) classes into javascript objects (string version).

Installation
------------

[](#installation)

With composer, make the dependecy required in your project:

```
composer require 10quality/php-class-to-javascript
```

Usage
-----

[](#usage)

Add trait to the wanted class:

```
use TenQuality\Traits\CastJavascriptTrait;

class MyClass
{
    use CastJavascriptTrait;
}
```

The user the casting methods:

```
$class = new MyClass;
$class->toJS();
$class->to_js(); // Alias
```

### Hide properties

[](#hide-properties)

To hide properties on casting, add the `hidden` property to the class:

```
class MyClass
{
    use CastJavascriptTrait;

    protected $hidden = [
        'propertyA',
        'property_2',
    ];
}
```

### Properties selection

[](#properties-selection)

To select a specific selection of properties to cast, add the `castingProperties` property to the class:

```
class MyClass
{
    use CastJavascriptTrait;

    // (1) As array
    protected $castingProperties = [
        'propertyA',
        'property_2',
    ];
}
```

```
class MyClass
{
    use CastJavascriptTrait;

    protected $attributes = [
        'id',
        'name',
    ];

    // (1) As property mapping
    protected $castingProperties = 'attributes';
}
```

Coding guidelines
-----------------

[](#coding-guidelines)

PSR-4.

LICENSE
-------

[](#license)

The MIT License (MIT)

Copyright (c) 2017 [10Quality](http://www.10quality.com).

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~4 days

Total

2

Last Release

3410d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f2cdad4e398292477582ba8ec4ab02a96fa645cd620518a5bcf697cd788c96c3?d=identicon)[amostajo](/maintainers/amostajo)

---

Top Contributors

[![amostajo](https://avatars.githubusercontent.com/u/1645908?v=4)](https://github.com/amostajo "amostajo (3 commits)")

---

Tags

phpjavascripttraitcast

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/10quality-php-class-to-javascript/health.svg)

```
[![Health](https://phpackages.com/badges/10quality-php-class-to-javascript/health.svg)](https://phpackages.com/packages/10quality-php-class-to-javascript)
```

###  Alternatives

[kkszymanowski/traitor

Add a trait use statement to existing PHP class

1305.6M16](/packages/kkszymanowski-traitor)[zakirullin/mess

Convenient array-related routine &amp; better type casting

21330.0k2](/packages/zakirullin-mess)[hedii/laravel-date-range

A date range trait with local scope methods for Laravel Eloquent models

2140.6k](/packages/hedii-laravel-date-range)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
