Im trying to inherit a constant var which are in another file/class. But I tried almost everything and I can't do it for some reason, can anyone tell me what is wrong or just are happening in my code please?
this is my main class that i called BasePage.
class BasePage extends CI_Controller {
const $TITLE = "My Own DB";
}
and this is the other class/file which is trying to inherit to other class by giving the constant var TITLE into an array.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require 'BasePage.php';
class Home extends BasePage {
public function index()
{
$data = array('title' => $TITLE);
$this->load->helper('url');
$this->load->view('page_login', $data);
}
}
The constant should not have a $ at the beginning.
And you don't access it like $TITLE
, or just TITLE
, but with self::TITLE
. In this case it would be most correct to use parent::TITLE
, since it belongs to a parent class.
This should work:
class BasePage extends CI_Controller {
const TITLE = "My Own DB";
}
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require 'BasePage.php';
class Home extends BasePage {
public function index()
{
$data = array('title' => parent::TITLE);
$this->load->helper('url');
$this->load->view('page_login', $data);
}
}