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
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);
Hi David,
You and your friend are not alone in thinking this would be a nice
feature to have, and the good news is that a few people have already
worked on proof-of-concept implementations.
Most notably, there's https://github.com/nikic/scalar_objects which has
a "register_primitive_type_handler" function almost identical to your
example.
A few people have taken that and played around with what a good set of
methods to implement would be, e.g.
https://github.com/rossriley/php-scalar-objects and
https://github.com/RikudouSage/scalar-objects
Right now, there's no concrete plan for if/how/when this might end up a
proper part of the language, though. It might be a good idea to search
one of the archives of this list to see where previous discussions ended up.
Regards,
Rowan Collins
[IMSoP]
Right now, there's no concrete plan for if/how/when this might end up a
proper part of the language, though. It might be a good idea to search
one of the archives of this list to see where previous discussions ended
up.
https://nikic.github.io/2014/03/14/Methods-on-primitive-types-in-PHP.html
appears to also be a worthwhile reading.
--
Christoph M. Becker