I have some classes and methods which are containing some php codes. Now I want to use those PHPcodes both for ajax and http requests. How?
Should I write all my PHP codes twice? onetime for ajax requests and one time for http request?
Here is my currect structure:
/********************************************** Search.php ****/
... // some html codes
<div id="content">
<?php
if(!empty($_SERVER["HTTP_X_REQUESTED_WITH"]) &&
strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) === "xmlhttprequest")
{
$type = true; // true means ajax request
} else {
$type = false; // false means http request
}
$obj = new classname;
$results = obj->func($type);
echo $results;
?>
</div>
... // some html codes
/********************************************** Classname.php ****/
class classname {
public function func($type){
$arr = ('key1'=>'some', 'kay2'=>'data');
if ($type){
echo json_encode($arr);
} else {
return $arr;
}
}
}
Now I want to know, this is standard? Actually I want to use it also for a request which came from a mobile app (something like an API). I think the above code has two problem:
- there will be a lot of IF-statement just for detecting type of requests (in the every methods)
- there is just one file (class.php) for both ajax and http requests. And when I send a ajax request by for example mobile, it will process some codes which doesn't need to them at all
Well, is there any point that I need to know?