Welcome Guest, Not a member yet? Register   Sign In
my model's '_construct' function has issues with 'new'
#1

[eluser]Teks[/eluser]
Please, someone tell me what I'm doing wrong.

I have a simple page_model file for a 'Page_model' class, which is:
Code:
class Page_model extends Model {
  
  public $title;  
  
  function _construct(){
    //calling the parent constructor:
      parent::_construct();
      $this->title = 'TEST TITLE';
  }
}

I then create a 'Page' controller, whose job is just to create a new page_model object, and pass it on to the view:
Code:
class Page extends Controller {
  
  function index(){
    
    $this->load->model('page_model');
    $data = new Page_model();
    
    $this->load->view('page_view', $data);
    
  }
}

The 'page_view' file, as you can expect, is very, very simple - all it has to do is show the $title variable from the page_model:
Code:
<html>
  <head>
      <title><?= $title ?></title>
  </head>
  
  <body>
    <p>The page's title is: "&lt;?= $title ?&gt;"</p>
  &lt;/body&gt;
&lt;/html&gt;

For some reason, however, the $title instance variable is not being set by the class' _constructor method, and the title comes up empty in the view.

I have already done some testing, trying to figure out what is happening.

1) I used class_exists() in the controller, after $this->load->model, to ensure that the class description was actually being included. It is - in the controller, after loading the model, "class_exists('Page_model')" returns TRUE.

2) I used the instanceof operator to ensure that the $data variable was being created as an instance of the Page_model class - it is: in the controller, "$data instanceof Page_model" returns TRUE.

3) Finally, I used isset() to check whether $data->title was being set by the _constructor function - and it isn't. In the controller, "isset($data->title)" returns FALSE.

So, it seems to me, that when '$data = new Page_model' is being called, the _construct function is either not being called, or it is for some reason failing to set the $title of the object.

Am I missing something really shamefully obvious?
#2

[eluser]Murodese[/eluser]
__construct() (double underscore)
#3

[eluser]Teks[/eluser]
[quote author="Murodese" date="1228066790"]__construct() (double underscore)[/quote]

Duuuuh!!!

Never have I been SO glad to be SO stupid! ;-P

Many, many thanks!




Theme © iAndrew 2016 - Forum software by © MyBB