Titanium app breaks with ajax call trying to redeclare class
I have an ajax request that requests a php script that connects to my server and returns some results
Here is my PHP code:
<?php
require_once 'p/DataBase.class.php';
if (!isset($db)) //i added this if statment incase this caused the error
$db = new DataBase();
$db->query('select * from table where status = 0 order by created_at desc');
$xml = ''; //$xml is just a variable, im not passing the data as xml, i was originally and didnt change the var name
while ($data = $db->fetchObject()) {
$xml .= $data->title . "<br />";
$xml .= $data->created_at . "<br />";
}
echo $xml;
?>
And here is the JQuery:
$(document).ready(function () {
function runAjax (data_obj,callback){
$.ajax({
url:"ajax.php",
dataType: "html",
data: data_obj,
success: function(html) {
if ( typeof(callback开发者_StackOverflow中文版) == "function") {
callback(html);
}
}
});
}
jQuery.timerDelayCall({
interval: 10000,
repeat: true,
callback: function(timer) {
runAjax({
content: $('#date').html()
}, function(result){
$('#updates').html(result);
});
}
});
});
This works fine locally and works the for the first ajax call in the titanium app, but the second call in the titanium app returns a fatal error: Cannot redeclare class DataBase in C:\path-to-app\dist\win32\app-name\Resources\p\DataBase.class.php on line 10 (line 10 being the class decleration class DataBase{
)
Any one got any ideas as to why this might be
Cheers
Luke
So. It seems to be a Titanium bug. I have solved it. When you declare any class/function, etc, you have to: 1 - move your functions and classes into some files 2 - define any constant after require_once your file(s) 3 - require_once your files only if not defined your constant
For example: i have an AJAX backend called test.php that uses a function from my library lib.php. Then i have to write the following: In my lib.php:
define('LIBRARY_INCLUDED',1);
function directoryToXml($directory) {/*bla bla bla*/}
In my test.php:
if(!defined('LIBRARY_INCLUDED')) {
require_once(dirname(__FILE__).'/lib.php');
}
It happens because Titanium uses PHP file not just like web server does it. The file with its classes and functions exists during the runtime and calling it twice causes to repeated declaration...
精彩评论