drslump / Protobuf-PHP

PHP implementation of Google's Protocol Buffers with a protoc plugin compiler
http://drslump.github.com/Protobuf-PHP/
MIT License
461 stars 163 forks source link

Add support for underscores in names #29

Closed vrecan closed 5 years ago

vrecan commented 11 years ago

We have fields that start with underscore that get mangled during code generation.

drslump commented 10 years ago

The behaviour of the camelize function is to provide accessor methods which feel more natural to common PHP code conventions.

It will provide the following transformations:

I don't see how this solves the problem for fields starting with an underscore. Perhaps it would be better to change the camelize function to the following:

if ($name[0] === '_') return $name;
return preg_replace_callback(
                '/_([a-z0-9])/i',
                function($m){ return strtoupper($m[1]); },
                $name
             );

Which will leave unmangled any field starting with an underscore, providing the following transformations: