nikic / scalar_objects

Extension that adds support for method calls on primitive types in PHP
MIT License
1.13k stars 44 forks source link

How will this work alongside shared libraries? #19

Closed TRPB closed 9 years ago

TRPB commented 9 years ago

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.

nikic commented 9 years ago

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 9 years ago

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()

terah commented 9 years ago

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.