1

I'm using PyroCMS with PHP 5.4.15 in my development server, so I installed a new module (created by someone else) and get this warning all the time:

A PHP Error was encountered

Severity: Warning

Message: Creating default object from empty value

Filename: admin/newsletters.php

Line Number: 63

This is the line 63:

$this->data->pagination = create_pagination('admin/newsletters/index', $total_rows);

So reading at PHP doc and Google seems to the var $this->data isn't initialized so in the constructor of the clase I declared as follow:

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

        $this->data = "";
}

But the error still appearing, so my question are:

  1. What's wrong in my solution?
  2. If I declare or define a var in the class constructor this var is initialized or not? (this is the related OOP part)
Community
  • 1
  • 1
Reynier
  • 2,420
  • 11
  • 51
  • 91

1 Answers1

1

You do not need to initialise like you would in functional PHP. Define it in your class is enough for it to exist in the object, for you to use as you like.

Class extends Parent {
  public $data;

  public function __construct() {
    parent::__construct();
    $this->data = 'Hello, world!';
  }
}

Used as...

$new = new Class();
echo $new->data;
// Hello, world!

and similarly...

$new = new Class();
$new->data = 'Goodbye, cruel world.';
echo $new->data;
// Goodbye, cruel world.
lukeocodes
  • 1,192
  • 1
  • 16
  • 31
  • I defined the var as you suggested I mean `public $data` and then try to use inside a function (not constructor) in this way `$this->data->pagination = create_pagination('admin/newsletters/index', $total_rows);` but still getting the same error – Reynier May 16 '13 at 21:02
  • `$this->data->pagination` is not defined in the object... Try and define `public $pagination` and do `$this->pagination = create_pagination('admin/newsletters/index', $total_rows);` – lukeocodes May 16 '13 at 21:05
  • can't do it in that way because the object I take is `data` then iterate trough values, how I remove the noising warning? I know ini_set is that the way? – Reynier May 16 '13 at 21:08
  • Do not remove warnings. Warnings and errors should not be ignored. Notices can be ignored (but try not to). I think what you need is an associative array for your pagination. `$this->data['pagination'] = create_pagination('admin/newsletters/index', $total_rows);` This might be a duplication of this: http://stackoverflow.com/questions/1949966/php-strict-standards-creating-default-object-from-empty-value-how-to-fix – lukeocodes May 16 '13 at 21:09
  • Thanks maybe I need to change the whole code in order to avoid this error – Reynier May 16 '13 at 21:17