简体   繁体   English

如何在AJAX调用中调用php函数而不是php类

[英]How to call php functions and NOT php class in an AJAX call

Here is a very simple and silly question I'm going to ask: 这是一个非常简单而愚蠢的问题,我要问:

I'm very new to PHP, I was wondering if there is a way to call just a function in a class of PHP by AJAX. 我是PHP的新手,我想知道是否有一种方法可以通过AJAX调用PHP类中的函数。

for instance something like this: 例如这样的事情:

$.ajax({
        url:'newPHPClass.php/My_Function_Name'
        ...
});

So in php, it would be like: 所以在php中,它会像:

<?php
class newPHPClass {
    function My_Function_Name(){

    }
}
?>

Why do I need this? 我为什么需要这个? So I wouldn't have like so many php files. 所以我不会喜欢这么多的php文件。 Instead I'll have a single php class with different functions inside. 相反,我将拥有一个内部具有不同功能的php类。 It will look cleaner and less extra files. 它看起来更干净,文件更少。

I have used ASP.NET MVC C# before. 我之前使用过ASP.NET MVC C#。 and in ASP.NET I could just simply put my controller's name in the URL section of AJAX followed by / and then my function's name (which could be modified in my Global.asax.cs file). 在ASP.NET中我可以简单地将我的控制器名称放在AJAX的URL部分中,然后是/然后是我的函数名称(可以在我的Global.asax.cs文件中修改)。

Thanks. 谢谢。

<?php

$action = $_POST['action'];

class Foo {

    function execute($action) {
        switch($action) {
            case "method1":
                $this->doMethod1();
                break;
            case "method2":
                $this->doMethod2();
                break;
        }
    }

    function doMethod1() {
        echo "Foo::doMethod1";
    }

    function doMethod2() {
        echo "Foo::doMethod2";
    }
}

$foo = new Foo();
$foo->execute($action);

?>

And use foo.php?action=method1 as URL. 并使用foo.php?action=method1作为URL。

First of all you don't call functions or methods via AJAX, you make a request to a page. 首先你不要通过Ajax 调用的函数或方法,您网页的请求。 If you make an AJAX call to a PHP file like: 如果您对PHP文件进行AJAX调用,例如:

<?php
class newPHPClass {
    function My_Function_Name(){

    }
}
?>

nothing will be returned and nothing will happen server side other than allocating the space for that class and then immediately destroy it at the end of the request. 什么都不会被返回,除了为该类分配空间然后在请求结束时立即销毁它之外,服务器端不会发生任何事情。

For your question: it doesn't really matter on the client side (Javascript) how you are going to manage the class and files organization: only one file is going to be requested by an AJAX call. 对于你的问题:在客户端(Javascript)如何管理类和文件组织并不重要:AJAX调用只会请求一个文件。

I'd suggest you to have at least two different files: the declaration of functions or classes and the file that manage the response. 我建议您至少有两个不同的文件:函数或类的声明以及管理响应的文件。 You need a bit a separation from execution and declaration, therefore I'd avoid things like: 你需要与执行和声明分开一点,因此我会避免这样的事情:

class A { ... }
$a = new A;
$a->myMethod();

Depending on the AJAX output I'd go with something like this instead: 根据AJAX输出,我会改为:

# class.php
class A {...}

# ajax.php
$a = new A;
$a->myMethod();

Finally a suggestion: don't use classes when they just don't make sense. 最后一个建议:当他们没有意义时,不要使用课程。 Having a bunch of functions grouped inside a class doesn't really feel OOP to me. 将一堆函数分组到一个类中并不会让我真正感受到OOP。 I think you are using classes with procedural code in mind. 我认为您正在使用具有过程代码的类。 Therefore I would go with a file that declares a bunch of functions instead. 因此,我会使用一个声明一堆函数的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM