定制 scrawler/php2js 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

scrawler/php2js

Composer 安装命令:

composer require scrawler/php2js

包简介

Simple library to convert php into frontend javascript code

README 文档

README

Convert php code to javascript code that can run on browser

Installation

package can be installed via composer

composer require piqon/php2js

Usage

If You want to create a copiled javascript file , you can run the following code:

<?php
include __DIR__ . '/vendor/autoload.php';

/*
* Replace test.php with path of your input php file
* Replace test.js with path for your js file output
*/

\PHP2JS\PHP2JS::compileFile(__DIR__.'/test.php',__DIR__.'/test.js');

If you just want to convert a small block of code , you can run the following code:

<?php
include __DIR__ . '/vendor/autoload.php';

$code = "
$name='1';
echo 'hi'; 
"

\PHP2JS\PHP2JS::compile($code);

Demo

Input code :

<?php
$name = 'Pranjal';
$cars = ['BMW','Audi'];
$cars->push('Ferrari');
echo $name;
echo $cars;

function click(){
    echo 'button clicked!';
}

Output code:

let name = 'Pranjal';
let cars = ['BMW', 'Audi'];
cars.push('Ferrari');
console.log( name);
console.log( cars);

function click()
{
    console.log( 'button clicked!');
}

Note

This was designed to convert php scripts to javascript code , this might not work with full blown php classes ! You can call javascript functions like console.log etc right from your php code and it will work as expected in the browser. This compiler does not support magic variables and magic functions of PHP.

Changelog

v0.3.0

  • Added support for import : include 'test' now converts to import test from './test.js' You can also use import_from() php function to define path of module. Example:
import_from('test','./component/test.js');

will compile to

import test from './component/test.js';

  • Added support for async function declaration via comment:
// @async
function abc(){
return 'hi';
}

will compile to

async function abc(){
return 'hi';
}

统计信息

  • 总下载量: 12
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固