使用 go 和仓颉重构了整个 PHP

53 天前
 2024

折言(origami-lang)

折言(origami-lang) 是一门创新性的融合型脚本语言,深度结合 PHP 的快速开发基因与 Go 的高效并发模型。同时还有部分 go 、ts 习惯引入。

⚠️ 当前状态

当前未对代码分支进行任何优化,性能尚未优化。 请作为一个工具使用,请勿用于生产环境。

🚀 核心特征

🎯 Go 反射集成

语法融合

特殊语法

数组方法

面向对象

📝 示例

Go 反射集成

// 定义 Go 结构体
type Calculator struct {
    Name string
}

func (c *Calculator) Add(a, b int) int {
    return a + b
}

func (c *Calculator) GetName() string {
    return c.Name
}

// 注册到脚本域
vm.RegisterReflectClass("Calculator", &Calculator{})
<?php
// 在脚本中使用
$calc = new Calculator(Name: "MyCalc");
echo $calc->GetName();     // 输出: MyCalc
echo $calc->Add(5, 3);     // 输出: 8
?>

函数注册

// 注册 Go 函数
vm.RegisterFunction("add", func(a, b int) int { return a + b })
vm.RegisterFunction("isEven", func(n int) bool { return n%2 == 0 })
<?php
// 脚本中调用
$result = add(5, 3);     // 返回 8
$even = isEven(4);       // 返回 true
?>

基础语法

int $count = 0;
string $name = "World";
echo "Hello {$name}";

function greet(string $name): string {
    return "Hello " . $name;
}

参数后置语法

function div($obj) {
    return "<div>" + $obj->body + "</div>";
}

function span($obj) {
    return "<span>" + $obj->body + "</span>";
}

$html = div {
    "body": span {
        "body": "内容",
    }
}

泛型类

class Users {
    public $name = "";
}

class DB<T> {
    public $where = {};

    public function where($key, $value) {
        $this->where[$key] = $value;
        return $this;
    }

    public function get() {
        return [new T()];
    }
}

$list = DB<Users>()->where("name", "张三")->get();

异步协程

function fetchData($url: string): string {
    // 模拟网络请求
    sleep(1);
    return "Data from " . $url;
}

// 启动异步协程
spawn fetchData("https://api.example.com");

echo "Main thread continues...\n";

HTML 内嵌

$content = <div class="container">
    <h1>{$title}</h1>
    <p>This is embedded HTML</p>
</div>;

数组操作

$numbers = [1, 2, 3, 4, 5];
$doubled = $numbers->map(($n) => $n * 2);
$evens = $numbers->filter(($n) => $n % 2 == 0);

中文编程

函数 用户(名称) {
  输出 名称;
}
用户("张三");

🚀 快速开始

git clone https://github.com/php-any/origami.git
cd origami
go build -o origami .
./origami script.php

📚 文档

💬 讨论群

📄 许可证

MIT 许可证

4344 次点击
所在节点    分享创造
49 条回复
2024
53 天前
@stabc 额,我手机给你回复的!无服务器是可以在 go 成开启端口,有请求来再解析脚本域代码,解析过程可以选择共享 vm 或者克隆隔离 vm ,就能做的全部代码是否持久化。go 和 php 是一体的,他们可以互相调用实现路由功能,如果你偏要认为实现不了就算啦
stabc
53 天前
@2024 为了性能面向 vm 编程哪来的“快速开发”呢?或者每次 REQUEST 都重新解析一边脚本里的函数,不是又回到了 PHP5 了么?
2024
53 天前
@stabc 可选
SethShi
53 天前
@2024 原生 PHP 在命令行就支持多线程,swoole 在 web 都支持协程了
Steaven
53 天前
牛,有意思
Gilfoyle26
52 天前
PHP:人生第二春
2024
50 天前
@seth19960929 #44 尝试把 php 添加真的异步功能,一直有人尝试的,但官方没有打算,https://www.cnblogs.com/zx-admin/p/19002183
https://externals.io/message/128053

这些都是最近的讨论
phithon
49 天前
如果能完全实现 php 语法,并跑过官方测试,作为 php 解释器,其实真是有需求的,我以前一直在寻找类似的方案但没有找到。
但如果不完全实现,只是一个“新语言”的话,感觉可能就不太可用了
2024
29 天前
@phithon #48 新增工具导出 go 库,https://github.com/php-any/generator

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1149147

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX