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;
    }

...

user2513149
  • 850
  • 12
  • 15
user1746582
  • 579
  • 1
  • 9
  • 20
  • Could you post the stack trace and / or the full function / class code? It would help us pinpoint the problem hopefully :) – Dan Matthews Jan 04 '13 at 09:36
  • Also i've just noticed - is this inside a static function? `$this` will not work from within static functions. – Dan Matthews Jan 04 '13 at 09: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 – user1746582 Jan 04 '13 at 09:51
  • Added my answer, give it a try, hopefully that should sort you out :) – Dan Matthews Jan 04 '13 at 09:55

1 Answers1

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.

Dan Matthews
  • 1,245
  • 2
  • 12
  • 24
  • 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?! – user1746582 Jan 04 '13 at 09:57
  • 1
    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. – Dan Matthews Jan 04 '13 at 09:59
  • See the answer at http://stackoverflow.com/questions/5734011/php-5-4-closure-this-support for an example. – Dan Matthews Jan 04 '13 at 10:00
  • btw just tried your suggestion and got the following error: Unhandled Exception Message: Cannot use $this as lexical variable – user1746582 Jan 04 '13 at 10:02
  • @user1746582 ah, okay, i've edited the solution to fix this, hopefully. – Dan Matthews Jan 04 '13 at 10:04
  • Hi Daniel, great stuff! upgrading Live to PHP 5.4 did the trick! thank you :-) – user1746582 Jan 04 '13 at 10:12
  • It's important to declare the method that you are trying to access as public, otherwise you will not be able to access it, because you are cloning it and accessing it from outside. – nikoskip Jan 29 '14 at 03:42