百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

scalar_objects

> 编程语言
开源

扩展,添加了对 PHP 中原始类型方法调用的支持

1.1K stars0 点赞2 次浏览
访问官网GitHub

工具介绍

扩展,添加了对 PHP 中原始类型方法调用的支持

Add support for method calls on primitive types in PHP

This extension implements the ability to register a class that handles the method calls to a certain primitive type (string, array, ...). As such it allows implementing APIs like $str->length().

The main purpose of this repo is to provide a proof of concept implementation that can be used to design the new APIs. The switch to object syntax for operations on primitive types is a unique opportunity for PHP to redesign many of its inconsistent core APIs. This repo provides the means to quickly prototype and test new APIs as userland code. Once the APIs are figured out it will be proposed for inclusion into PHP.

Note: The ability to register type handlers from userland is just for prototyping. It's not something I would actually want in PHP in the end.

Registering type handlers

Type handlers are registered through register_primitive_type_handler. The function takes a type name (like "string" or "array") and a class name. The class should contain static methods, which receive the primitive type as the first parameter:

length()); // int(3)
var_dump($string->startsWith("a")); // bool(true)

The valid type names are: null, bool, int, float, string, array and resource. Not all of those will make sense in practice, but for now they are all supported.

Implemented APIs

As already pointed out in the introduction the main purpose of this repo is designing good APIs for the primitive types. The implemented APIs are available in the handlers/ folder (and are obviously work in progress). In order to load these APIs just include the handlers/bootstrap.php file.

Installation

The master branch supports PHP version 7.0 to 8.1. The extension is incompatible with the JIT compiler.

Unix

In order to compile and install the extension run the following commands:

phpize
./configure
make
sudo make install

Windows

Download a prebuilt Windows DLL that matches your PHP version and move it into the ext/ directory of your PHP installation. Furthermore you'll have to add extension=php_scalar_objects.dll to your php.ini.

Testing the extension

The extension comes with a run-tests.php file to run the tests. (To see examples of the implemented APIs you should also look in the tests.) The script is run as follows:

php run-tests.php -q -p php

Where php is the path to your PHP executable.

Limitations

This extension has a number of limitations:

  • Due to technical limitations, it is not possible to create mutable APIs for primitive types. Modifying $self within the methods is not possible (or rather, will have no effect, as you'd just be changing a copy).

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言