0

PHP classes should normally be in a file by themselves. Static PHP methods can be called within PHP without instantiation of the class as below:

<?php

class TestClass
 {
   static function myStaticFunction()
     {
        return "The uninstantiated return value";
     }
 }

 echo TestClass::myStaticFunction();
 // echos "The uninstantiated return value"

It is easy to use a Javascript ajax call to a PHP script that instantiates a PHP class and runs a class method.

My question is: Is it possible to call a PHP static class method from Javascript using ajax thus eliminating the need for an intermediate PHP script or instantiation of the class, while not violating the PHP good practice of classes being in separate files all to themselves.

I tried putting the call to the PHP static method in the class constructor and got an error message. Putting all of the functionality in the constructor would also defeat the utility of the class for other purposes.

If this is possible please show how one would call a static PHP class method using ajax without using an intermediary script. If this has been answered before please provide a link to the answer before closing this question as I have not found an answer directly on point.

If this is impossible, why is it that PHP can call a static class method without instantiation, but Javascript cannot do the same via ajax?

7
  • I am not sure what you want but PHP script execute on server side and JavaScript execute on client (browser) side. Commented Feb 8, 2016 at 20:33
  • 1
    I would say that having the javascript dictate what exactly gets called in PHP is bad practice because essentially, you are giving the user the ability to try and call anything in your code without the correct parameters, and could potentially harm your server. Commented Feb 8, 2016 at 20:33
  • Can you update your question in order to make clear which class you are talking about when you said: "I tried putting the call to the PHP static method in the class constructor and got an error message"? Commented Feb 8, 2016 at 20:35
  • Show how you would do this by instantiating the class and it may be more clear. Commented Feb 8, 2016 at 20:35
  • 1
    Javascript is done on the client while PHP is being executed on the server, which is why we use AJAX to set up a HTTP request and tell the server to execute PHP file X. PHP file X does then what we want to do. Commented Feb 8, 2016 at 20:36

1 Answer 1

0

See: AJAX request and PHP class functions for a similar, but not identical, question with several answers.

It appears that PHP can call a static class function without instantiation of the class, but js ajax cannot do the same. At least, that is the consensus from most all of your answers.

Many people responded suggesting the intermediate script, but that was not the question. The question was how to avoid the intermediate script.

https://stackoverflow.com/users/1006348/rajesh saw the problem best in the other posting.

https://stackoverflow.com/users/1311025/tomas-prado suggested the best answer but discounted the value of the answer in the other posting. He suggested one intermediate script that can handle any ajax call that requires only instantiating one class and one class method with included data. I have not tried it, but I think that will work. No need for 50 intermediate scripts. Each ajax call will call the same PHP intermediate script. The data will name the class and the method and supply the remaining needed data. The universal intermediate script will parse out the class and method and send the data package there. The class method will be designed to ignore the first two fields in the data because they are only for routing info.

Thanks for all of the answers. If anyone has a better answer please let us all know.

jimfuqua

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.