0

So I have this code snippet.

function get_box_ajax() {
    global $box;

    $box = get_box(); // creates box object

    ob_start();
    get_template( '/filepath/content.php' );
    $output = ob_get_clean();
}

// in the content.php file

global $box;

<form action="<?php echo box_url( $box->url ); ?>" method="post"> // error on this line
...
</form>

So with this code, I am getting a non-object error on the call to $box->url. Take note this is done via ajax.

So I thought in my ajax function I have already globalized $box and that will take but it doesn't seem to work? Any thoughts?

16
  • This code doesn't make much sense... when is get_box_ajax() called? Commented Jul 31, 2013 at 23:34
  • 2
    Somebody please correct me if I'm wrong, but aren't global variables with the use of global keyword scoped per request? I can't seem to find the docs for it. And another point to note is that every ajax request is considered different request. Commented Jul 31, 2013 at 23:37
  • Well, get_box_ajax() creates the $box object right? so that kind is relevant since that's your error.. Commented Jul 31, 2013 at 23:37
  • @vinodadhikary +1 That seems like a good idea where the confusion is.. Commented Jul 31, 2013 at 23:38
  • 1
    @Rick Show us the code for get_box() then Commented Jul 31, 2013 at 23:50

2 Answers 2

1

Two things:

When is your get_box_ajax function called? And what does the function get_box do? Both things are relevant.

I don't think the problem is whether box is global or not (which it is), but rather if the url variable of box is being defined or if box is being initialized at all.

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

4 Comments

This is not the issue as everything works in my code..I get what I expect back from the ajax call albeit the error i get as well...I get all the content back because the $box->url has a fallback if nothing is passed...
@Rick Try doing a var_dump($box); after your global $box; declaration. The error is not in what you showed us but rather in what you didn't show (e.g. the get_box function or the AJAX request).
Another words, in my AJAX function I can do this echo $box->url and it would give me the URL...
So I am getting an impression that this has something to do with the output buffering...but am not sure
0

Set box to null before the function. There's no reference in the global scope

$box = null;
function get_box_ajax() {
    global $box;

    $box = get_box();

Change the start to that

8 Comments

sorry I don't follow, can you please elaborate.
why would that solve the problem "non-object error on the call to $box->url" ?
Because box isn't in the global scope so its not an object. It you make it null outside of the function. The function then over rides and the full reference is in the global scope
First, that's not right, you don't need to set global variable to null to use it. Second that doesn't fix the problem with $box not being an object when using $box->url, that function isn't called..
No not to null. But it needs to be set. Null seemed appropriate. and if its not in scope then its not an object. Look at the docs all examples the variable is first declared outside of the function and not inside ...
|

Your Answer

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