nikic/scalar_objects

How will this work alongside shared libraries?

TRPB opened this issue · 3 comments

TRPB commented

I love the idea of this extension and would love to see it included in PHP by default, however I can foresee a couple of problems.

Someone writes a template library that registers a handler for strings, someone else writes a ORM which does the same. Using both inside my own project is now going to cause a problem isn't it? If the template library has a different $str->replace() implementation or doesn't implement some features requires by the ORM one would override the other and cause breakage. Essentially this would be action-at-a-distance because libraries could accidentally break each other.

My first thought at a solution would be to add a third parameter to register_primitive_type_handler that was the name of a namespace (Which in an ideal world would also apply to subnamespaces) that way, the template library could call register_primitive_type_handler('string', 'StringHandler', '\TemplateLibrary'); and the ORM could call register_primitive_type_handler('string', 'StringHandler', '\ORMLibrary'). register_primitive_type_handler would then only apply to strings that were created inside the relevant namespace.

I'm just thinking aloud here, I have no idea how difficult this change would be or even whether it's possible to implement.

The plan for inclusion in core (should it ever happen ^^) is that PHP provides an API and everybody has to use that. The register_primitive_type_handler stuff is there so we can experiment with this writing PHP code, instead of writing C code.

TRPB commented

aha! Makes perfect sense :) I would love to see this included in PHP7 not only is it a much better way of working ($int->abs() is better than abs($int) because it can be called on an $int variable that is scalar int or some sort of BCMath implementation, for example) but it could also be used as an opportunity to fix the inconsistent naming conventions str_replace and strpos become $str->replace() and $str->pos()

I'd also like to be to see this in the core complete with something like https://github.com/rossriley/php-scalar-objects go in with it.