Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to resolve 'Creating default object from empty value' warning in CodeIgniter

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 2.61k
    Comment on it

    Coder missed a very simple fix and general good practice that you should always initialize your object before you try to set a property. It is very simple fix for this is simply to add a new StdClass; call right before the error with the variable it is trying to access.

    CodeIgniter

    <?php
        //bad code
        $this->db->insert('user', $this);
        $userID=$this->db->insert_id();
    
        $data->user_id    = $userID;
        $this->db->insert('user_profile', $data);
    ?>
    
    <?php
        //good code
        $this->db->insert('user', $this);
        $userID=$this->db->insert_id();
    
        $data = new StdClass;
        $data->user_id    = $userID;
        $this->db->insert('user_profile', $data);
    ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: