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 用三极管设计一个单管共射放大电路
  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架