I have been writing some code in PHP for a bit of time, but I am still pretty new on it.

I have a script with all functions I need for my webapp. The script looks like this?

function foo(bar){...}
function bar(foo){...}

And the script that handles the user requests from the front end is like so:

include 'script1';
if($_POST["function"] == "foo"){
  echo json_encode ( foo($_POST["param"]) );

Is this a good/correct way to relate my REST script with my functions script? Should I use requests from a script to another? Is there any more elegant or faster way of doing this?

  • 1
    Unless you're doing this as an exercise on how to write a web framework. I would highly suggest using Laravel or another framework like Lumen. It will have enough tooling and be opinionated enough to safeguard you from having to make these kind of decisions. Nov 26 '19 at 19:07
  • 1
    Or consider following the Symfony 'create your own framework' tutorial. Creating the framework step by step for yourself might help you understand some of what goes in to a framework and why. symfony.com/doc/current/create_framework/introduction.html
    – bdsl
    Nov 26 '19 at 21:13

You're asking about how to route requests to the appropriate function. This is a big topic, and it's usually a good idea to use an off the shelf routing library. You could search online for php routing library and pick one that has a good reputation and you find convenient to work with.

As for linking two PHP scripts together, so that you can define functions in one and call them in the other, the standard way is to use autoloading and composer. If you install and use composer, and combine all your functions into a PHP class, PHP will be able to detect when it needs to load the file that defines the class, and load it automatically. See instructions at https://medium.com/tech-tajawal/php-composer-the-autoloader-d676a2f103aa .

If for any reason you can't use autoloading, it's usually better to use require_once instead of include. This will stop the script immediately if it can't load the file it needs, which should make it easier to spot the problem. It will also keep track of whether it's loaded the script already, and not load it again. It's very common in a more complex project to end up requiring one file for multiple reasons, but if it just defines classes and functions there's no need to actually load it more than once.


You may consider to write a code generator. It should

  • read the first script and find all the "function" lines, parse them with some regex

  • extract the relevant signature information for all functions

  • use this information to write the code for the second script into a code file.

If you have the scripts under your control, this probably does not have to be overly robust and can be solved in a pragmatic way. So I guess you don't have to make it a full-fledged PHP parser, only something which can solve this task by making some assumptions how the parsed code looks like in your environment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.