dongtou9934 2013-09-03 21:19
浏览 132
已采纳

PHP 5.3.2替代在匿名函数中使用$ this?

I am using Laravel 4 and PHP to build a new application. It works fine on my dev server running PHP 5.4.x however my boss insist that it has to run version 5.3.2

I have spent the whole day fixing everything to work with 5.3.2 and almost have everything, so I thought, until I ran into an issue with the code below.

My problems start at this line...
DB::transaction(function($clock_in_webcam_image) use ($clock_in_webcam_image)

I believe this type of code might not work with this version of PHP? If that is the case, what are my options to run this same code or have it doing the same action?

Would appreciate any help with this. Very unfortunate that my boss told me straight out that no he will not allow us to update to a newer PHP so I am stuck in a hard spot right now

// Create a new time card record when a User Clocks In
public function createTimeCard($clock_in_webcam_image) {
    // Create both Timecard and timecard record tables in a Transaction
    DB::transaction(
        function ($clock_in_webcam_image) use ($clock_in_webcam_image) {
            $timeCard = DB::table('timeclock_timecard')->insertGetId(
                array(
                    'user_id'               => $this->user->user_id,
                    'clock_in_datetime'     => $this->dateTime->format($this->dateFormat),
                    'clock_in_timestamp'    => $this->dateTime->getTimestamp(),
                    'clock_in_webcam_image' => $clock_in_webcam_image
                )
            );

            $timeCardPunchEntry = DB::table('timeclock_punch_entry')
                ->insertGetId(
                    array(
                        'timecard_id'          => $timeCard,
                        'user_id'              => $this->user->user_id,
                        'created_at_datetime'  => $this->dateTime->format($this->dateFormat),
                        'created_at_timestamp' => $this->dateTime->getTimestamp(),
                        'clock_type'           => 'clock_in',
                        'webcam_image'         => $clock_in_webcam_image
                    )
                );

            return $timeCard;
        }
    );
}

UPDATE

In response to bansi's comment...is this what you mean to do...

DB::transaction(function() use($myModel){
    $myModel->updateTable1();
    $myModel->updateTable2();
})

展开全部

  • 写回答

2条回答 默认 最新

  • duanhongyi2964 2013-09-03 22:59
    关注

    Before PHP 5.4.0, you could not use $this inside an anonymous function. There is a simple workaround though where you can use the use construct to pass variables into the functions scope. Also, you are using the use construct incorrectly as $clock_in_webcam_image is not defined in the parent scope.

    $user = $this->user;
    $dateTime = $this->dateTime;
    
    DB::transaction(function($clock_in_webcam_image) use ($user, $dateTime) {
        // snip
    
        array(
            'user_id' => $user->user_id,
            'clock_in_datetime' => $dateTime->format($this->dateFormat),
            'clock_in_timestamp' => $dateTime->getTimestamp(),
            'clock_in_webcam_image' => $clock_in_webcam_image
        )
    
        // snip
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?