dongtou9934
2013-09-04 05:19
浏览 129
已采纳

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条回答 默认 最新

相关推荐 更多相似问题