1

I am using Laravel and I have just moved my code that is working locally into Live and am getting the exception below within my 'User' controller:

Unhandled Exception
Message:
Using $this when not in object context

The strange thing is, that this IS a class and works locally fine so I don't expect the solution to be using the static notation. It's only when I promote this to Live that I get this error. Could it be that something in the Laravel core that is missing from Live that is not loading the controller class properly?

Has anyone experienced this after promoting their code to Live? any ideas?

UPDATED: snippet of code where error is occuring. remember this code works locally so i believe something is missing rather than this code needs to be changed to fix this issue.

class User_Controller extends Base_Controller {

...

public function action_register() {
   ...
    if ($user) {
        //Create the Contact
        DB::transaction(function() use ($user_id) {
            $org = $this->create_org($user_id); //failing on this line with exception. btw $user is created fine
            $this->create_contact($org->id);
            $this->create_address($org->id);
    });

    private function create_org($user_id) {
        $result = Org_type::where('name','=',$_POST['org_type'])->first();

        $org = Org::Create(
            array(
                'name' => $_POST['org_name'],
                'user_id' => $user_id,
                'org_type_id' => $result->id,
            )
        );
        return $org;
    }

...

4
  • Could you post the stack trace and / or the full function / class code? It would help us pinpoint the problem hopefully :) Commented Jan 4, 2013 at 9:36
  • Also i've just noticed - is this inside a static function? $this will not work from within static functions. Commented Jan 4, 2013 at 9:41
  • thanks Daniel, just updated with code snippet. No the reference to $this is withing a controller function which is not static. and it works fine locally on my laptop :-0 Commented Jan 4, 2013 at 9:51
  • Added my answer, give it a try, hopefully that should sort you out :) Commented Jan 4, 2013 at 9:55

1 Answer 1

4

It seems the problem is that you're using $this inside a Closure you're providing to the DB::transaction function, i'm not sure why it would be working on live local, but you must import the instance of the controller into the function to use it.

The best way to do that, to avoid confusion, would be to alias it too, and possibly pass it by reference so you're not copying it, like:

$this_var = $this;
DB::transaction(function() use ($user_id, &$this_var as $controller) {
        $org = $this->create_org($user_id); //failing on this line with exception. btw $user is created fine
        $controller->create_contact($org->id);
        $controller->create_address($org->id);
});

I'm not entirely sure if the syntax is perfect, but the logic is sound.

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

7 Comments

thanks Daniel although it doesn't explain why it is working locally and not when I send the code to Live? I am thinking something is missing from Live though not sure what? Based on your answer, shouldn't it also be failing locally?!
It should, yes. But if you're running PHP5.4 locally, it seems that they support $this, and if you're host is running PHP5.3, that would explain the difference.
See the answer at stackoverflow.com/questions/5734011/… for an example.
btw just tried your suggestion and got the following error: Unhandled Exception Message: Cannot use $this as lexical variable
@user1746582 ah, okay, i've edited the solution to fix this, hopefully.
|

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.