consolidating php interface files for ajax calls
Combininng PHP files used in AJAX call
Two or More Files
<?php // p1.php
require_once 'p0.php';
$a = new signin();
?>
<?php // p2.php
require_once 'p0.php';
$su = new signup();
?>
I call about 6 files like this for AJax calls. Question that I'm wondering is can I combine them into s开发者_开发问答omething like below were I add an extra paramter in along with the POST data to determine which Objects to instantiate? Would this be better practice? Actually I'm pretty sure I can do this so instead of having pi1.php, pi2.php, pi3.php .... I would have one file called say pi.php which dynamially instantiates objects based upon the user action. Is this good practice? I don't like having too many files so this what I plan to do. I'll just pass a POST parameter to AjaxInterface which deterines which objects to instantiate.
One File
<?php // pi.php
require_once 'p0.php';
$a = new AjaxInterface();
?>
The question you must ask yourself is: Is it reasonable to call these functions in groups? That is, are these functions related and I'll probably want to call #1-4, or are they unrelated except for their short lengths.
If these are related functions, then it makes sense to put them in one file, and use a parameter to ask for the specific functions. An example call to this file to execute three different methods might be:
example.com/ajax/doCaeser.php?come&see&conquer
If these are different choices of the same super-object or same interface, it also might make sense to group them. In that case, you would want to make calls of this nature:
example.com/ajax/objectProducer.php?helmet
However, if this is purely to limit the number of files, don't. If you are concerned about many files, give them better names so that you can visually group them. Good:
authSignin.php
authSignup.php
authSignout.php
authAdmin.php
clearlyNotRelatedFile.php
Bad:
p1.php
p2.php
p3.php
p4.php
c5.php
It would be a better practice to put code they share between them into include files and then have six distinct different PHP files for servicing the requests. Why put six web services into one big call when you can have six well-named distinct ones?
精彩评论