I and a friend are thinking about PHP to treats raw types as objects.

Currently we have some raw types on PHP, like strings, ints, arrays,
etc. Generally we should use some procedural functions to works with
this types, for instance, strtolower(string). It works in general, but
the most of languages treats this types as objects. JS, for instance,
you should do String.toLower().

We can pass this string to a constructor to allow we treat it as
object. But it should overload PHP if we do it with all raw types that
we have. We have some Spl that do exactly that for array, for
instance, but we should use with caution.

In this case, I suggests to have some handler to treat raw types as
objects in an indirect way. Objects are not created, but it methods
are called on demand statically.

For instance:

// It should be declared by PHP internally (default handler).
register_type_handler('string', \SplString::class);

// It is an user defined handler, should extends SplInteger.
register_type_handler('int', \Types\MyInt::class);

class MyInt extends \SplInt {
    public static function max($max) {
        // $this is the own int
        return max($this - 1, $max);
    }
}

// Let's use:
echo $string->lower(); // native defined function
echo $int->max(5); // user defined function
echo $int->min(2); // native defined function

I think that it should not create any BC and not implements any new
operator. Performance should be not affected, once it should be
triggered only when type is called as object, and it should implements
the same function implemented in procedural version. Should affects
performance only if you have a user-defined method, because it will be
done by PHP, not internally.

The major advantage, in this case, is that you can works with OO over
raw types and fixes the old PHP issue about function naming (no more
str_ vs str).

It too should avoid the matroska, by allowing call chained by default:
$string->lower()->ucfirst().

There are someone that could write some code over PHP code to test the
viability and performance? It should be great. In all case, depending
of response here, I could try do something, but I don't promises.

-- 
David Rodrigues

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to