1

I just got tired to find out, why the below warning message is generated with my Codeigniter Project.

A PHP Error was encountered

Severity: Warning

Message: Creating default object from empty value

Filename: controllers/person.php

Line Number: 268

In My person Controller,

function __construct()
    {
        parent::__construct();

        // load library
        $this->load->library(array('table','form_validation'));

        // load helper
        $this->load->helper('url');
        $this->load->config('array_constant', TRUE);

        // load model
        $this->load->model('Person_model','',TRUE);
    }
// set empty default form field values
    function _set_fields()
    {
        Line No: 268 $this->form_data->id = '';
        $this->form_data->name = '';
        $this->form_data->gender = '';
        $this->form_data->dob = '';
        $this->form_data->district = '';
        $this->form_data->education = '';
        $this->form_data->occupation = '';
        $this->form_data->annual_income = '';
        $this->form_data->company_name = '';
        $this->form_data->description = '';
    }

In My View file,

<table width="100%">
          <tr>
            <td width="30%">ID</td>
            <td><input type="text" name="id" disabled="disable" class="text" value="<?php echo set_value('id'); ?>"/></td>
            <input type="hidden" name="id" value="<?php echo set_value('id',$this->form_data->id); ?>"/>
        </tr>
        <tr>
            <td valign="top">Name<span style="color:red;">*</span></td>
            <td><input type="text" name="name" class="text" value="<?php echo set_value('name',$this->form_data->name); ?>"/>
<?php echo form_error('name'); ?>
            </td>
        </tr>

Any idea or suggestions to suppress the above warning!

echo "<pre>";
        var_dump($this->form_data);
        echo "</pre>";

object(stdClass)#21 (10) {
  ["id"]=>
  string(0) ""
  ["name"]=>
  string(0) ""
  ["gender"]=>
  string(0) ""
  ["dob"]=>
  string(0) ""
  ["district"]=>
  string(0) ""
  ["education"]=>
  string(0) ""
  ["occupation"]=>
  string(0) ""
  ["annual_income"]=>
  string(0) ""
  ["company_name"]=>
  string(0) ""
  ["description"]=>
  string(0) ""
}
8
  • 1
    can you try var_dump($this->form_data); please and report here the dump? Commented Jan 1, 2013 at 16:28
  • I do notice you aren't setting the 2nd argument for set_value('id'); not sure if that is what is causing the problem. Commented Jan 1, 2013 at 16:29
  • Which line is line #268 in person.php? Commented Jan 1, 2013 at 16:31
  • Thanks for your quick reply.Hi Badaboooooom, I have updated my question. Commented Jan 1, 2013 at 16:34
  • Hi cryptic, Yes the line 268 in person.php Commented Jan 1, 2013 at 16:37

2 Answers 2

4

This notice is generated in PHP 5.4 and up. It's not an error, it's a notice - intended to help you debug your application and raise awareness of possible problems.

When you call _set_fields initially, $this->form_data is NULL, then you attempt to treat a NULL like an object with this line:

$this->form_data->id = '';

NULL can't have properties like "id", so the notice is telling you that PHP assumed you wanted an object, so it made one automatically for you.

There are a lot of ways to solve this that entail some restructuring of your code, but a simple solution to silence the notice:

$this->form_data = new stdClass;
$this->form_data->id = '';
// etc.

A better solution is to use a class for your object, very simple example:

$this->form_data = new MY_Form_Data_Object();

class MY_Form_Data_Object { // use a better name that describes what this is
    public $name = '';
    public $gender = '';
    public $dob = '';
    // etc.
    // add some helpful methods here when needed, like validation
    // maybe a constructor to initialize some values
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wow. Its great... I used your simple solution to suppress the warning! Again thanks for your great suggestion.
2

I had the same issue, except that i was not able to change all my code.

To let CI suppress the warning i set the ENVIRONMENT to production in the index.php:

//define('ENVIRONMENT', 'development');
define('ENVIRONMENT', 'production');   

Basically CI sets the error reporting

error_reporting(0); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.