Been writing more PHP code lately, coming from a Java background, I like having getters/setters despite PHP typically just using public variables. A while back, I found this site that helps to generate code, but it was lacking in some features; especially when I preferred the pascal/camel case on my functions when the variables were named in snakecase.
snakecase – words in a variable are separated by _ (e.g. my_variable)
camelcase – words are alternated by capitalized letters (e.g. myVariable)
pascalcase – similar to camel, but the first letter is capitalized also (e.g. MyVariable)
kebabcase – words are separated by – (e.g. my-variable)
The drawbacks of the previous solution: Michael Angstadt, would take private/protected variables, regardless of what type of naming convention and use a simple implementation that would just add the get/set in front of that variable to create the function.
What I wanted was to be able to transform snakecase (this is typically used in python, some api’s also seem to prefer it) to pascal case on the function names so that I wouldnt have to manually go in and change it. Also many times, I like having a constructor, which is tedious depending on how many variables are in the model. Also when outputting to excel, it’s nice to have a toArray function.
Unfortunately, it’s not a 100% code generation because PHP doesn’t really do type hinting, there is no way to really know if the variable is supposed to be a string or number or array – but most of the time, strings are good enough, so this generator outputs assuming that the variable is a string, which still requires some manual modifications, but it will handle 80% of the work.
Perhaps later in the future, I could build something to account for the different types. Other possible additions might be to generate a function to translate json into the model class.
Check it out here!