npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

js-php-generator

v0.1.0

Published

Generate PHP code programmatically from JavaScript. A port of nette/php-generator.

Downloads

13

Readme

JavaScript PHP Generator

A JavaScript/TypeScript library for generating PHP code programmatically. This is a port of the popular nette/php-generator package.

Features

✅ Generate PHP classes, interfaces, traits, and enums
✅ Support for PHP 8.5 features including property hooks, enums, attributes, etc.
✅ Modify existing classes programmatically
✅ PSR-12 / PER coding style compliant output
✅ TypeScript support with full type definitions
✅ Node.js and browser compatibility

Installation

npm install js-php-generator

Quick Start

import { ClassType, PhpNamespace, PhpFile } from 'js-php-generator';

// Create a simple class
const class_ = new ClassType('Demo');
class_
  .setFinal()
  .setExtends('ParentClass')
  .addImplement('Countable')
  .addComment('Class description.\nSecond line\n')
  .addComment('@property-read Nette\\Forms\\Form $form');

// Generate PHP code
console.log(class_.toString());

Examples

Basic Class Generation

import { ClassType } from 'js-php-generator';

const class_ = new ClassType('User');
class_
  .setFinal()
  .addProperty('name')
    .setType('string')
    .setVisibility('private')
    .setValue('John Doe');

class_
  .addProperty('age')
    .setType('int')
    .setVisibility('private')
    .setValue(30);

class_
  .addMethod('getName')
    .setVisibility('public')
    .setReturnType('string')
    .setBody('return $this->name;');

console.log(class_.toString());

Namespace Support

import { PhpNamespace, ClassType } from 'js-php-generator';

const namespace_ = new PhpNamespace('App\\Models');

const userClass = namespace_.addClass('User');
userClass
  .addProperty('id')
    .setType('int')
    .setVisibility('private');

const orderClass = namespace_.addClass('Order');
orderClass
  .addProperty('userId')
    .setType('int')
    .setVisibility('private');

console.log(namespace_.toString());

PHP File Generation

import { PhpFile, PhpNamespace, ClassType } from 'js-php-generator';

const file = new PhpFile();
file.addComment('This file is auto-generated.');
file.setStrictTypes();

const namespace_ = file.addNamespace('App\\Controllers');
const controller = namespace_.addClass('UserController');
controller
  .addMethod('index')
    .setVisibility('public')
    .setReturnType('array')
    .setBody('return [];');

console.log(file.toString());

Advanced Features

import { ClassType } from 'js-php-generator';

const class_ = new ClassType('AdvancedDemo');

// Add attributes (PHP 8.0+)
class_
  .addAttribute('Route', ['/api/users'])
  .addAttribute('ApiResource');

// Add constructor
const constructor = class_.addMethod('__construct');
constructor
  .addParameter('name')
    .setType('string')
    .setRequired(true);
constructor
  .addParameter('age')
    .setType('int')
    .setDefaultValue(18);

// Add typed properties
class_
  .addProperty('items')
    .setType('array')
    .setVisibility('private')
    .setValue([]);

console.log(class_.toString());

API Reference

Core Classes

  • ClassType - Represents a PHP class
  • InterfaceType - Represents a PHP interface
  • TraitType - Represents a PHP trait
  • EnumType - Represents a PHP enum
  • PhpNamespace - Represents a PHP namespace
  • PhpFile - Represents a complete PHP file
  • Method - Represents a PHP method
  • Property - Represents a PHP property
  • Parameter - Represents a method parameter
  • Attribute - Represents a PHP attribute

Utility Classes

  • Printer - Base printer for generating PHP code
  • PsrPrinter - PSR-12 compliant printer
  • Dumper - Variable dumper for generating PHP code
  • ClassManipulator - Tools for manipulating existing classes

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

This project is a JavaScript port of nette/php-generator by Nette Framework.