【发布时间】:2011-02-02 14:45:33
【问题描述】:
我是一位经验丰富的程序员,第一次接触 PHP 和 Ajax,但在弄清楚如何将面向对象的 PHP 合并到我的 ajax webapp 中时遇到了一些麻烦。
我有一个管理页面 (admin.php),它将根据用户在管理页面上选择的表单从 XML 文件加载和写入信息 (info.xml)。我决定使用一个对象(ContentManager.php)来管理 XML 文件到磁盘的加载和写入,即:
class ContentManager{
var $xml_attribute_1
...
function __construct(){
//load the xml file from disk and save its contents into variables
$xml_attribute = simplexml_load_file(/path/to/xml)
}
function get_xml_contents(){
return xml_attribute;
}
function write_xml($contents_{
}
function print_xml(){
}
}
我像这样在 admin.php 中创建 ContentManager 对象
<?php
include '../includes/CompetitionManager.php';
$cm = new CompetitionManager()
?>
<script>
...all my jquery
</script>
<html>
... all my form elements
</html>
所以现在我想使用 AJAX 来允许用户通过 ContentManger 应用程序使用这样的接口 (ajax_handler.php) 从 XML 文件中检索信息
<?php
if(_POST[]=="get_a"){
}else if()
}
...
?>
我明白如果我不使用对象,这将如何工作,即hander php 文件将根据.post 请求中的变量执行特定操作,但是通过我的设置,我看不出我怎么能获取对我在 ajax_handler.php 文件中的 admin.php 中创建的 ContentManager 对象的引用?也许我对 php 对象范围的理解是有缺陷的。
无论如何,如果有人能理解我正在尝试做的事情,我将不胜感激!
【问题讨论】: