dongtou9934 2013-09-04 05: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-04 06: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条)

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败