Why do I need to use the parent::__construct(); constructor, what does it have I need?
//CONTROLLER
class users_ctrl extends CI_Controller {
function __construct() {
parent::__construct(); //Why do I need to include it?
$this->load->model('select_model');
}
public function index()
{
$data['user_list'] = $this->select_model->get_all_users();
$this->load->view('show_users', $data);
}
}
//MODEL
class select_model extends CI_Model{
function __construct() {
parent::__construct();
}
function get_all_users()
{
$query = $this->db->get('students');
return $query->result();
}
}
In your given examples, you are calling the load class that the base controller class loads. Without the parent's constructor, you would have to load an instance of it yourself.
As for your model, you would have to manually load your db object.
Remove it and you should get something like called to undefined propery $class::load
CodeIgniter aside, it really is a basic fundamental of object-oriented programming.
If you make a class that extends another, and you declare a new constructor in the child class, the parent constructor will never run - since you've overridden it, and since CodeIgniter base controller does (most likely) a lot of things behind the scenes, if you do not run parent constructor, your controller most likely won't be injected in CI's container.
Related
$this is use for current class and view is method but what is load. Is this a property?
Is this example correct?
class super{
public $property;
public function superf1()
{
echo "hello";
}
public function col()
{
$this->superf1();
}
$this->property->super1();
}
Yes, load is a property.
Think of it like this:
class Loader {
public function view() {
//code...
}
}
class MyClass {
private $load;
public __constructor() {
$this->load = new Loader();
}
public someMethod() {
$this->load->view();
}
}
This syntax is called chaining.
Your controller inherits CI_Controller. So, if you look in application/system/core/Controller.php you'll find something interesting : $this->load =& load_class('Loader', 'core'); (l.50 with CI2). So, $this->load refer to the file application/system/core/Loader.php which have a function public function view (l.418 with CI2)
In the context of a class that extends CI_Controller (in other words: a controller) the symbol $this is the Codeigniter "super object". Which is, more or less, the central object for CI sites and contains (among other things) a list of loaded classes. load is one of the classes you'll always find there because it is automatically loaded by the CI system.
Technically, the class creates objects of the type CI_Loader. view() is just one of the many methods in the load class. Other commonly used class methods are model(), library(), config(), helper(), and database(). There are others.
So, in short, load is a class used to load other resources.
load is a class belongs to the loader class
codeigniter official documentation
view, model and others are methods
In PHP 8.1
use return("viewname", $data)
I have been using CI for two years now. One thing that really annoys me is the use of &get_instance(). Although it is halpful while we are inside library , helper , presenters , model etc. But everytime loading it is cumborsome. If you forget loading it somewhere and simply use $this->blah->blah() instead of $CI->blah->blah() this makes too much trouble and if you are working online you face the client who is complaining that he sees the error. I have seen in the laravel that you does not need to load the instance anywhere throughout the application. This is because laravel is autoloading all the libraries and models and both are available anywhere in the application. But this seems to me disadvantage why loading classes that are not required in some particular places. This tells me Codeigniter is flexible but still i want an alternative where i dont want to use &get_instance(). Any idea or suggestion ? Please.
In your model or Core model or library
//class MY_Model extends CI_Model
//class SomeLibrary
class Some_model extends CI_Model {
private $_CI;
public function __construct() {
parent::__construct(); //for model or core model
$this->_CI =& get_instance();
}
//if you called attributs who does not exist in that class or parent class
public function __get($key)
{
return $this->_CI->$key;
}
//if you called methods who does not exist in that class or parent class
public function __call($method, $arguments)
{
call_user_func_array(array($this->_CI, $method), $arguments );
}
public function test() {
var_dump($this->some_controller_key);
var_dump($this->some_lib_loaded);
}
}
*NOT TESTED YET
Inspired by an piece of code from the awesome Flexi Auth
//from a Model to keep access of CI_Controller attributs
public function &__get($key)
{
$CI =& get_instance();
return $CI->$key;
}
I was shocked when I saw that ^^
To explain the &__get, i think when you will call this magic method a second time PHP will do not execute it again, but will take his result from the first call.
One stupid question, I want to call from my admin_news controller function which resides in another controller Admin. FUnction name is is_logged_in();
admin.php
public function is_logged_in()
{
....
}
admin_news.php
public function __contruct()
{
parent::__construct();
//admin->is_logged_in();??
}
how can I do that?
Thanks
You will have to move that functionality somewhere else, Codeigniter's architecture doesn't allow multiple controller instances in one request. You have multiple options like using a common base class, libraries, helpers and so on.
I would recommend you to create your own MY_Controller base class (see Extending Core Classes) and put your method there, like this:
class MY_Controller extends CI_Controller {
protected function is_logged_in() {
// ...
}
}
Once you have it there you can call it like:
class AdminNews extends MY_Controller {
public function __construct() {
parent::__construct();
$this->is_logged_in();
}
}
I am having trouble trying to call CodeIgniter methods in my static functions, just using $this doesn't work because its not in object context, the static keyword doesn't work either. This an example of the code in my core model, the $table variable is successfully defined from another model like posts.
class MY_Model extends CI_Model {
protected static $table;
public function __construct() {
parent::__construct();
}
public static function find_all() {
$this->db->select('*');
$sql = $this->db->get(static::$table);
return $sql->result();
}
}
If $this doesn't work you can get around this like this:
$CI =& get_instance();
$CI->db->...
The codeigniter built in loader class automatically instantiates the class. There is no support to use classes without instantiating. You can manually include your file in the model file then you can use it. For more details check out this thread:
http://codeigniter.com/forums/viewthread/73583/
What you want is a refence to the static var in the class, so use:
class MY_Model extends CI_Model {
protected static $table;
public function __construct() {
parent::__construct();
}
public static function find_all() {
$this->db->select('*');
$sql = $this->db->get(self::$table);
return $sql->result();
}
}
And, of course, $table does need to have a value!
Codeigniter doesn't generally support static methods I believe and they are really an attempt to shoehorn procedural code into object oriented code.
Anyway, I think your best bet is to either use the class without static methods, or turn your code into a "helper". A helper is just an old school function library. You would create it under the helpers folder and it can be loaded with $this->load->helper('helper_name'). You can then call the function as in normal procedural code, in other words 'find_all()'
Hope this helps.
First time contributor :)
Is it possible to call the member function of another controller in zend framework, if yes then how?
<?php
class FirstController extends Zend_Controller_Action {
public function indexAction() {
// general action
}
public function memberFunction() {
// a resuable function
}
}
Here's another controller
<?php
class SecondController extends Zend_Controller_Action {
public indexAction() {
// here i need to call memberFunction() of FirstController
}
}
Please explain how i can access memberFunction() from second controller.
Solution
Better idea is to define a AppController and make all usual controllers to extend AppController which further extends Zend_Controller_Action.
class AppController extends Zend_Controller_Action {
public function memberFunction() {
// a resuable function
}
}
class FirstController extends AppController {
public function indexAction() {
// call function from any child class
$this->memberFunction();
}
}
Now memberFunction can be invoked from controllers extending AppController as a rule of simple inheritance.
Controllers aren't designed to be used in that way. If you want to execute an action of the other controller after your current controller, use the _forward() method:
// Invokes SecondController::otherActionAction() after the current action has been finished.
$this->_forward('other-action', 'second');
Note that this only works for action methods (“memberAction”), not arbitrary member functions!
If SecondController::memberFunction() does something that is needed across multiple controllers, put that code in a action helper or library class, so that both controllers can access the shared functionality without having to depend on each other.
You should consider factoring out the code into either an action helper or to your model so that it can be called from both controllers that need it.
Regards,
Rob...
I would suggest you to follow DRY and move those functions to common library place. For example create in library folder
My/Util/
and file
CommonFunctions.php
then call your class
My_Util_CommonFunctions
and define your methods
Now you can call them from any place in the code using your new namespace which you have to register.
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace(array('My_'));
in any controller you can call your custom methods by using:
My_Util_CustomFunctions::yourCustomMethod($params);