Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8039559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:36:32+00:00 2026-06-05T03:36:32+00:00

I am using php 5.3.3 and developing a MVC webapp using codeigniter. The code

  • 0

I am using php 5.3.3 and developing a MVC webapp using codeigniter. The code I am trying to refactor is basically bunch of:

$this->db->trans_start();
    // do some db updates
    $this->db->update(...);
    $this->db->update(...);
$this->db->trans_complete();

if ( $this->db->trans_status() == FALSE ) {
    $this->handle_db_error();
}

so I have a few of the above code scatter around in my model classes. I would like to refactor the transaction handling part out of the models and stay DRY.

I thought I could using closure like the following:

// in parent CI_Model class 
class MY_Model extends CI_Model {
  public function txn_wrap($closure) {
    $this->db->trans_start();
    $closure();
    $this->db->trans_complete();
    if ( $this->db->trans_status() == FALSE ) {
        $this->handle_db_error();
    }
  }

// in child model class

 class Blog_model extends MY_Model {
   protected $_table = 'app_blog';

   public function get($id) {
     $instance = $this;
     $closure = function() use($instance, $id) { 
        // do some db updates
        $instance->db->update($instance->_table, array('title' => 'bla'), array('id' => $id));
     };
     $this->txn_wrap($closure);
   }

That doesn’t work and gives me "PHP Fatal error: Using $this when not in object context". So I guess using of $this usage is not supported in 5.3.

Now failing of using closure, What else can I do?

UPDATE: now I am getting Undefined property: App_Blog::$_table where App_About is the controller which calls the get() function in Blog_model. I have tried function() use($instance, , $instance->_table, $id) but php complains about the syntax.. Now this closure thing doesn’t appears give me as much benefits as I thought..

thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:36:33+00:00Added an answer on June 5, 2026 at 3:36 am

    The closure is not a method of your child model class, so you cannot access the instance. You will need to provide it to the closure:

    $instance = $this;
    $table = $this->_table;
    $update_db_closure = function() use ($instance, $table) {
        // do some db updates
        $instance->db->update($table, ...);
        $instance->db->update($table, ...);
    };
    

    The $this->_table property is protected, thus you won’t be able to access it inside the closure, so you need to pass a copy of it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a application using PHP. Some example code is here. $url =
Thanks for your time. I am developing application using PHP. In this for some
I am developing a webapp using PHP and MongoDB. In this app I keep
I'm developing a website using PHP. My .htaccess has this rewrite rule: RewriteEngine On
I am developing a facebook application using php-sdk. i want to take some extended
I am currently developing a php application using MVC techniques. I started it without
i am developing websites using php on my local computer. after i finish some
I am developing an API using PHP (Codeigniter) and Phils RESTserver. I am creating
I'm developing a web-application using PHP which is on tests by some of my
I am developing a web application using php and mysql. This application runs on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.