Search code examples
phpcodeignitercodeigniter-2static-variables

How to access a public static variable in a model from a controller in Codeigniter?


Here is the model structure

class Misc_model extends CI_Model {

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

    public static $type_alphabet        = 'a';
}

I am accessing the variable from a Controller, like this:-

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once('webservice_common.php');
class Webservice_user extends Webservice_common {

    protected $_data = array();

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

    public function preRegistration(){
        $miscObj = new Misc_model;
        $type = $miscObj::$type_numeric;
    }
}

Is this the right way to do in Codeigniter, or is there some other way? In codeigniter, we load models like this:-

$this->load->model('misc_model');

And for calling a function we write like this:-

$this->misc_model->the_function();

Is there any other specific way (in Codeigniter) to access public static function from a different controller?


Solution

  • Try this

    Model:

    class Misc_model extends CI_Model {
    
        public function __construct() {
           parent::__construct();
        }
    
        public static $type_alphabet = 'a';
    }
    

    Controller:

    public function preRegistration(){
        $this->load->model('Misc_model');
        $type = Misc_model::type_numeric;
    }