PHP,jQuery和Ajax的面向对象 [英] PHP, jQuery and Ajax Object Orientation

查看:85
本文介绍了PHP,jQuery和Ajax的面向对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个非常有经验的程序员让我的头PHP和Ajax周围的第一次,和我有一点麻烦搞清楚如何将面向对象的PHP进入我的AJAX的web应用。

I'm a fairly experienced programmer getting my head around PHP and Ajax for the first time, and I'm having a bit of trouble figuring out how to incorporate object-oriented PHP into my ajax webapp.

我有一个管理页面(admin.php文件),将加载和写入来自取决于用户选择的管理页面上的表单的XML文件中的信息(info.xml)。我决定用一个对象(ContentManager.php)来管理XML文件保存到磁盘的负载和写作,即:

I have an admin page (admin.php) that will load and write information (info.xml) from an XML file depending on the users selection of a form on the admin page. I have decided to use an object (ContentManager.php) to manage the loading and writing of the XML file to disk, i.e :

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对象,像这样

I create the ContentManager object in admin.php like so

<?php
include '../includes/CompetitionManager.php';
$cm = new CompetitionManager()
?>
<script>
  ...all my jquery
</script>
<html>
  ... all my form elements
</html>

所以,现在我想用AJAX来允许用户使用像这样的接口(ajax_handler.php)通过ContentManger应用程序检索XML文件中的信息

So now I want to use AJAX to allow the user to retrieve information from the XML file via the ContentManger app using an interface (ajax_handler.php) like so

<?php
  if(_POST[]=="get_a"){

  }else if()
  }
  ...
 ?>

我知道如何做到这一点,如果我没有使用对象,即投手PHP文件会做一定的动作,取决于在。员额要求一个变量​​的工作,但我的设置,我不知道怎样才能有了一个指向我在admin.php文件中ajax_handler.php文件中创建的ContentManager对象?也许我的PHP对象范围的理解是有缺陷的。

I understand how this would work if I wasn't using objects, i.e. the hander php file would do a certain action depending on a variable in the .post request, but with my setup, I can't see how I can get a reference to the ContentManager object I have created in admin.php in the ajax_handler.php file? Maybe my understanding of php object scope is flawed.

无论如何,如果任何人都可以做出我想要做的意义上,我AP preciate一些帮助!

Anyway, if anyone can make sense of what I'm trying to do, I would appreciate some help!

推荐答案

觉得每一个AJAX调用作为一个单独的请求。如果在一个特定的请求的生命周期还没有实例化你的ContentManager,对象不存在。如果你想使用多个请求之间的一个对象,它序列化到会话,并在请求生命周期的早期反​​序列化。

think of each ajax call as a separate request. if in the life cycle of a particular request you have not instantiated your ContentManager, the object doesn't exist. If you'd like to use a single object between multiple requests, serialize it to session and deserialize it early in the request life cycle.

这篇关于PHP,jQuery和Ajax的面向对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆