Refactor php code

This a big refactor of common base php code for work more easy when the project grows.

See the message commits for more detailed info about each change. I expect that the changes are suitable for you.