Jump to content

A PHP Error was encountered Severity: Warning Message: Creating default object from empty value


Danny620

Recommended Posts

Everytime i run my script i get

A PHP Error was encountered

Severity: Warning

Message: Creating default object from empty value

Filename: controllers/user.php

Line Number: 71

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class User extends MY_Controller
{
	var $form_data;
    
    function __construct()
    {
        parent::__construct();
        
        // load model
        $this->load->model('User_model', '', TRUE);
		
		$this->lang->load('pubs');
		$this->load->helper('language');
        
    }
	
	public function register(){
		
        // load model
        $this->load->model('Pub_model', '', TRUE);
		         
		$data['title']     = 'Register';
		
		// set empty default form field values
        $this->_set_fields();
        // set validation properties
        $this->_set_rules();
		
		$data['city_options']     = $this->Pub_model->city_options();
        $data['county_options']   = $this->Pub_model->county_options();
        $data['facility_options'] = $this->Pub_model->facility_options();
		
		if ($this->form_validation->run() == FALSE){
		 
        // load view
        $this->load->view('includes/header', $data);
        $this->load->view('front/register', $data);
        $this->load->view('includes/footer', $data);
		
		}else{
			
			// build array for the model
            $form_data = array(
                'pub_id' => set_value('pub_id'),
                'room_title' => ucwords(strtolower(set_value('room_title'))),
				'room_description' => ucwords(strtolower(set_value('room_description'))),
                'adults' => set_value('adults'),
                'children' => set_value('children'),
                'price_per_night' => set_value('price_per_night')
            );
            
            if ($this->User_model->save($form_data)) {
                $this->session->set_flashdata('flash_success', 'Room was successfully added');
            } else {
                $this->session->set_flashdata('flash_success', 'Room was not added successfully');
            }
			
			// redirect to room list page
            redirect('user/register/', 'refresh');
			
		}
		
	}
	
	// set empty default form field values
    function _set_fields()
    {
    	
        $this->form_data->title           = '';
        $this->form_data->first_name      = '';
        $this->form_data->last_name       = '';
		$this->form_data->dob             = '';
        $this->form_data->email           = '';
		$this->form_data->password        = '';
		$this->form_data->passconf        = '';
        $this->form_data->landline        = '';
        $this->form_data->mobile  		  = '';
		$this->form_data->address_1       = '';
		$this->form_data->address_2       = '';
		$this->form_data->city_id         = '';
		$this->form_data->county_id       = '';
		$this->form_data->postcode        = '';
    }
	
	public function title_check($title_input)
	{	
		//allowed title items
		$titles = array("Mr", "Mrs", "Miss", "Ms", "Dr", "Prof");
		
		if (in_array($title_input, $titles)){
			return TRUE;
		}else{
			$this->form_validation->set_message('title_check', 'The %s field must be a value from the dropdown list');
			return FALSE;
		}
	}
	
	public function account_check($account_input)
	{	
		//allowed account types
		$types = array("3", "4");
		
		if (in_array($account_input, $types)){
			return TRUE;
		}else{
			$this->form_validation->set_message('account_check', 'The %s field must be a valid account type');
			return FALSE;
		}
	}
    
    // validation rules
    function _set_rules()
    {	
		$this->form_validation->set_rules('account_type', 'Account Type', 'callback_account_check');
		$this->form_validation->set_rules('title', 'Title', 'callback_title_check');
		$this->form_validation->set_rules('first_name', 'First Name', 'required|trim|alpha|xss_clean|max_length[50]');
		$this->form_validation->set_rules('last_name', 'Last Name', 'required|trim|alpha|xss_clean|max_length[50]');
        $this->form_validation->set_rules('dob', 'Date of Birth', 'xxxxxxxxxxxxxxxx');
		$this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[25]|matches[passconf]');
		$this->form_validation->set_rules('passconf', 'Password Confirm', 'required');
		$this->form_validation->set_rules('email', 'Email', 'trim|valid_email|max_length[100]');
		$this->form_validation->set_rules('landline', 'Landline', 'trim|is_numeric|max_length[20]');
		$this->form_validation->set_rules('mobile', 'Mobile', 'trim|is_numeric|max_length[20]');
		$this->form_validation->set_rules('address_1', 'Address Line 1', 'required|trim|xss_clean|max_length[150]');
		$this->form_validation->set_rules('address_2', 'Address Line 2', 'trim|xss_clean|max_length[150]');
		$this->form_validation->set_rules('city_id', 'City', 'required|is_numeric|max_length[5]');
		$this->form_validation->set_rules('county_id', 'County', 'required|is_numeric|max_length[5]');
		$this->form_validation->set_rules('postcode', 'Postcode', 'required|trim|xss_clean|max_length[16]');
		$this->form_validation->set_rules('country_id', 'Country', 'required|is_numeric|max_length[5]');
		
		//Set global error delimiters
		$this->form_validation->set_error_delimiters('<span class="error">', '</span><br />'); 
    }
}
/* End of file user.php */
/* Location: ./application/controllers/user.php */ 
Link to comment
Share on other sites

It's a warning because all your script does is declare $this->form_data as *a* property of your class/object, but doesn't initialize it as any particular type (specifically, some object type, since that's what you are using it as). Then in your function, you just start using it as an assumed object.

 

It's basically the equivalent of doing $array['someindex'] = 'foobar'; without first doing $array = array();. PHP is generally smart enough to assume your intentions based on context, but it will throw a warning at you about it if your error reporting is turned on to show it.

 

At a minimum, you should do this:

 

$this->form_data = new stdClass(); // init as a generic object
$this->form_data->title = '';
// etc...
Or this:

 

$this->form_data = (object) array(
  'title' => '',
  'first_name' => '',
  // etc..
);
Those will initialize $this->form_data as a generic/anonymous object type and keep the error from happening.

 

HOWEVER.. $this->form_data may or may not really supposed to be instantiated as some existing class in your framework (I can't answer that). If your script seems to be working fine, aside from that warning being shown, then it's probably safe to just initialize it as shown above.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.