• Stars
    star
    1,120
  • Rank 39,868 (Top 0.9 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 11 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Extension that adds support for method calls on primitive types in 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:

<?php

class StringHandler {
    public static function length($self) {
        return strlen($self);
    }

    public static function startsWith($self, $other) {
        return strpos($self, $other) === 0;
    }
}

register_primitive_type_handler('string', 'StringHandler');

$string = "abc";
var_dump($string->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).

More Repositories

1

PHP-Parser

A PHP parser written in PHP
PHP
16,432
star
2

FastRoute

Fast request router for PHP
PHP
4,986
star
3

iter

Iteration primitives using generators
PHP
1,108
star
4

php-ast

Extension exposing PHP 7 abstract syntax tree
PHP
900
star
5

PHP-Fuzzer

Experimental fuzzer for PHP libraries
PHP
394
star
6

Phlexy

Lexing experiments in PHP
PHP
160
star
7

DB

Very simple and secure PDO wrapper class
PHP
92
star
8

php-crater

Like crater, but for PHP
PHP
68
star
9

include-interceptor

Library to intercept and dynamically transform PHP includes. Forked from icewind1991/interceptor.
PHP
68
star
10

sample_prof

Sampling profiler for PHP
C
58
star
11

comparable

PHP extension implementing a magic "Comparable" interface
C
49
star
12

TypeUtil

Utility for adding PHP 7 scalar types and return types
PHP
47
star
13

ditaio

Cooperative task scheduler using coroutines
PHP
42
star
14

buffer

PHP extension for buffer based typed arrays
C
39
star
15

popular-package-analysis

Utilities to analyze most popular composer packages
PHP
38
star
16

nikic.github.com

CSS
36
star
17

llvm-compile-time-tracker

LLVM compile-tracking tracking infrastructure
PHP
33
star
18

Phuzzy

Fuzzer for PHP internal functions
PHP
31
star
19

prephp

Preprocesses PHP code to allow custom syntax and syntax of newer PHP versions, especially PHP 5.3
PHP
29
star
20

PHP-Backporter

Converts PHP 5.3 code to PHP 5.2 code
PHP
22
star
21

llvm-compile-time-data

LLVM compile-time performance data over time (repo 0).
18
star
22

tokenstream

Framework around PHP's Tokenizer to allow easier working with it
PHP
8
star
23

SPL-Datastructures

C
8
star
24

pear

Repo for publishing my PEAR packages
CSS
3
star
25

llvm-ir-diffs

LLVM
1
star
26

php-langspec

PHP Language Specification
PHP
1
star
27

test-repo

For testing things.
1
star