1

I have a PHP file, .../datastuff.php On this page a PHP object is instantiated. (The class is written in another file).

public class MyClass{

        //....
       public function doAfterClick(){
        //.....
       }
}

So in datastuff.php I do

$MyObject = new MyClass();
 //user specific data is added to $MyObject

Now I want to call $MyObject->doAfterClick(), when the user presses a button on datastuff.php

I think AJAX is the general solution for this type of problem. But I am not sure how to do this? If I wanted to call a PHP function written on another page I could use AJAX to POST data to that page. But I want to stay on this page, because MyObject needs to call the method.

If I POST back to datastuff.php won't $MyObject be lost? If I make it a Singleton or Global would this then work? I looked at this, which is a similar problem, ut is not quite what I am looking for: Using JQuery ajax to call a PHP file, process, but stay on same page

3
  • What is it you're actually trying to achieve? You can get the button to make another request to your server-side application (PHP code) but every request is handled independently so the $MyObject variable won't be the same. It may well be an identical copy to the initial request, but it's not the same object. A singleton or a global won't make any difference - these are only ways of getting the same object within the same request. Commented Nov 30, 2012 at 15:20
  • @RobMasters The object is necessary because it holds data and presents it to the user. The user must then confirm the data. If he clicks a confirm button, I want to call the object's method, which may be a database insert or showing other data or something like that. Commented Nov 30, 2012 at 15:24
  • This might be helpful: stackoverflow.com/questions/5403736/… Commented Nov 30, 2012 at 15:25

1 Answer 1

3

Here's the basic idea:

# datastuff.php

// This object will be created upon each request
$MyObject = new MyClass();

$action = (array_key_exists('action', $_GET)) ? $_GET['action'] : '';
switch ($action) {
    case 'do_something':
        $response = $MyObject->doAfterClick();
        // Output relevant feedback to the user or redirect them somewhere based on the response

        // This was most likely called via AJAX, so perhaps output JSON data or
        // some other means of communicating the response back to the javascript
        break;

    case 'do_something_else':
        // etc...
        break;

    default:

        // Present the information to the user...

        break;
}

Your $MyObject will be created upon each request (i.e. on initial page load and after the button is clicked and fires an AJAX request) but it may be identical in each request assuming it has been given the same data. After the button is clicked, an ajax request to "datastuff.php?action=do_something" will make sure the relevant method is called.

Although something like this would work, it is not good code design. I'd recommend you spend some time researching MVC (Model, View, Controller) patterns.

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

2 Comments

Looking into MVC is a good idea. Here is a video tutorial that helped me achieve pretty much exactly what you are trying to do.
I marked the answer as correct, in that I think I need to rethink the basic design, as you suggested.

Your Answer

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