Home  >  Article  >  Backend Development  >  How to call method from PHP class in browser

How to call method from PHP class in browser

PHPz
PHPzOriginal
2023-04-25 17:35:44754browse

As the Internet continues to develop, the needs of websites and applications are becoming more and more complex. In order to better meet the needs of users, developers have to constantly learn new technologies and tools. Among them, PHP is a very popular server-side scripting language used for developing dynamic websites and web applications. In PHP, classes are a very important program component that can package code logic into units that are easy to maintain and reuse. This article will introduce how to call methods through PHP classes in the browser.

First, you need to write a PHP class on the server side. Suppose we are developing a website for an online store and need to process user order information. We can write a PHP class called "Order" class for manipulating order data. This class can contain some methods, such as "addOrder", "removeOrder", "getOrderList", etc., for adding, deleting and obtaining order information.

Next, this class and other related PHP files need to be packaged into a complete web application on the server side. In this process, you can use some popular PHP frameworks, such as Laravel, Symfony, and CodeIgniter, to improve the maintainability and scalability of the code.

Once the web application is deployed to the server, it can be accessed in the browser. In order to call the methods of the "Order" class, some JavaScript code needs to be written on the client side. Since JavaScript is a front-end scripting language, it is usually used to handle interactive and dynamic effects on web pages. Therefore, JavaScript code written on the client side needs to use some tricks to call PHP classes on the server side.

A common method is to use the "XMLHttpRequest" object. This is a built-in JavaScript object used to send HTTP requests to the server and receive responses. By using the "XMLHttpRequest" object, you can issue an HTTP request in the browser, call the methods of the PHP class on the server side, and receive the returned data through the HTTP response.

The following is a sample code that uses the "XMLHttpRequest" object to call the "addOrder" method of the "Order" class:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    var response = JSON.parse(xhr.responseText);
    console.log(response);
  }
};
xhr.open("POST", "/order.php");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify({method: "addOrder", data: {id: 123, name: "张三", phone: "123456789"}}));

In the above code, an "XMLHttpRequest" object is first created, and A callback function is set. This callback function is called when an HTTP response is received and the returned data can be read from the response. Then, specify the type of HTTP request and URL address by calling the "open" method. Then call the "setRequestHeader" method to set the HTTP header information. Finally, the request data is sent to the server through the "send" method.

Next, in the "order.php" file on the server side, you can process this HTTP request, call the "addOrder" method of the "Order" class, and return the response data to the client. The following is the sample code of the "order.php" file:

<?php
require_once(&#39;Order.php&#39;);
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST[&#39;method&#39;])) {
    $order = new Order();
    $method = $_POST[&#39;method&#39;];
    $data = $_POST[&#39;data&#39;];
    if (method_exists($order, $method)) {
        $result = $order->$method($data);
        echo json_encode(array('status' => true, 'result' => $result));
    } else {
        echo json_encode(array('status' => false, 'message' => 'Method not found.'));
    }
} else {
    echo json_encode(array('status' => false, 'message' => 'Invalid request.'));
}
?>

In the above code, the definition file of the "Order" class is first introduced. Then, determine the type and parameters of the HTTP request, read the requested data, and call the corresponding method of the "Order" class. If the method exists, the execution result and status code are returned to the client through the JSON protocol. Otherwise, an error message is returned.

Through the above method, you can call the server-side PHP class method in the browser and return data through the HTTP response. This method allows front-end developers and back-end developers to collaborate to complete their respective work, improving the development efficiency and code quality of the project. At the same time, the risk of exposing sensitive data and code logic on the client can also be avoided.

The above is the detailed content of How to call method from PHP class in browser. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn