简体   繁体   中英

Proper PHP coding style

I'm working on a new project that is going to make heavy use of different AJAX calls to PHP. Is it proper to create one file that contains all the different functions and use a case statement to pick the proper one, or should I create a php folder and put each function in its own file?

I'm trying to get my skills back on track with modern web design standards and practices, and in the past, one file to rule them all was the way to go for simplicity sake; however, so much has changed that I wanted to ask before I got 10,000 lines into this project.

Thank you for any advice you can provide.

This question will illicit a lot of personal opinion.

Case and point, my opinion:

Code re-usability is key of any programming language. Store like functions together in an include file, include the type (or however else you organized your functions) in the main module in which you need those functions in. You shouldn't put one function per file, but at the same time one huge file with 400 functions in it would be a nightmare.

Just use a modern framework and the style it will impose on your coding.
and use an Opcode, which will enable you to write clearer code without impacting performance.
Some modern framework names will be Zend, Yii, Codeigniter etc

If I understand, it's totally fine to have all your functions in the same file (if you're making a really big site, you should probably divide them; like functions for this section, functions for that section).

I'm not sure why you would use a switch statement to pick them; sometimes it's useful but normally won't you just be calling the functions directly? Like include the "library" file, and call "getNewId()" whenever a user registers or something.

IMO it depends.

If your Ajax script is 10,000 lines, and you are handling dozens or hundreds of Ajax requests per second, and you are not using an opcode cache for PHP, you are going to waste a lot of time and resources loading such a large script into memory, parsing it, and executing a few lines from it.

If you have 2,000 lines, and a few dozen ajax calls per minute, then it's not as big a deal.

One file per function may be a bit overkill as well, perhaps there are some functions that could be grouped together and then you include the file when any of the functions related to that file are called.

Instead of thinking about the implementation of the problem why not think about the interface? This is what I mean, you could separate the real work to done in classes somewhere, and let the ajax make calls to another separate classes/function. Then the functions called through ajax can query the different classes/objects to accomplish the work and send respond back to ajax. It may sound like the MVC pattern. The advantage of this is clearly code reusability , improved decoupling and easier code maintenance. Example:

class Cook{

    function friedEgg(){/**/}
    function currySoup(){/**/}
    function sandwich(){/**/}
}

And function called through ajax could be

function ajaxBreakfast(){
    $cook = new Cook;
    $result = $cook->friedEgg();
    $result .= $cook->sandwich();
    echo $result;
}

function ajaxLunch(){
    $cook = new Cook;
    $result = $cook->currySoup();
    echo $result;
}

Hope you get my point!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM