Can not access to codeigniter superobject into the hooks and uri - php

I first call a super object and add this contractor
$this->CI =& get_instance();
but when I want to access
print_r($this->CI->router->fetch_class());
it shows the error
Trying to get property of non-object

Related

CodeIgniter 3: Loading a library on MY_Model

I have created a class MY_Model from which all my models inherit from. Inside it I have a method called switchConnection. Inside it I need to use system library encryption.
I know that in a model $this refers to the model so I can not do:
$this->load->library('encryption')
I have tried loading the library using:
$ci = & get_instance();
$ci->load->library('encryption');
$ci->encryption->decrypt(....);
but I get the same error: Trying to get property of non-object.
Any ideas?
I found a solution:
$this->load->library('encryption');
$myEncryption = new CI_Encryption();
$myEncryption->decrypt($string);
Class name is CI_Encryption
You should remove the space between '=' and '&'.
So try this way ->
$ci =& get_instance();

codeigniter $this dont load in controller

i have a problem im trying to deal with for a long time,
i have a codeigniter webapp that im building and i have a problem with the $this of codeigniter
i will show you with so examples:
this is in my controller
$this->site_model->blog_post('title');
this one work perfectly /\
if im loading an other / different model it will load it all good, but if im calling a function in the new model like this:
$this->Admin_model->blog_post('title');
it will give me this
Undefined property: Idx::$Admin_model
Call to a member function get_categories() on a non-object in
C:\dev\wamp\www\francebeautycoil\application\controllers\idx.php on line 22
if im doing the next code
public $ci = '';
$this->ci =& get_instance();
$this->load->model('Admin_model');
$this->ci->Admin_model->blog_post('title');
it will work.
ive tryed all the catital lettes, lowercase letters.
the thing is that its append not only on this model but on few libraries too.
please help, im stuck.
Every time you use an model you need to load it first , on second snipped you need to load Admin_model first
if im loading an other / different model
On second snppiet you are calling a method on Admin_model not loading it on line below
$this->Admin_model->blog_post('title');
so load it first
$this->load->model('Admin_model');
so it becomes
$this->load->model('Admin_model');
$this->Admin_model->blog_post('title');

codeigniter library within library issue

I am trying to call libraryA within libraryB in the constructor of the libraryB.
I am aware I can do:
$CI =& get_instance();
$CI->load->library('A');
$CI->A->someFunc()
However I cannot call $CI->A->someFunc() without initiating it with
$CI =& get_instance(); $CI->load->library('A'); in whatever function in B I am trying to access someFunc();. In other situations I would just be able to initiate it in the constructor $this->load->model('somemodel'); and call the model in any function within the class with $this->load->someFunc();. How to I achieve the same functionality without having to rewrite the block of code above over and over in each function I want to use it?
I'm not 100% clear this is what you're asking (your question is confusing), but you can save the CI instance as a property, then access it from anywhere in your class.
class My_library {
protected $CI;
public function __construct()
{
$this->CI =& get_instance();
$this->CI->load->library('other_library');
}
public function someMethod()
{
// Use the library (and the CI object) via the CI property.
$var = $this->CI->other_library->anotherMethod();
}
}

Error: undefined property $load

I am new to codeigniter, and I have developed a code to carry out queries on the database. I load the database using$this->load->database(); and perform a query, but when I run the code, the browser gives me the following error message:
A PHP Error was encountered Severity: Notice Message: Undefined property: Tutorial::$load.
Fatal error: Call to a member function database() on a non-object
This is the code I am using:
class Tutorial extends CI_Controller {
public function tutorial() {
$this->load->database();
$query = $this->db->query('SELECT user,pass,email FROM tablex');
foreach ($query->result() as $row) {
echo $row->title;
echo $row->name;
}
I am sure the $db variables in my database configuration file are properly set and I have even tried autoloading the database for all pages in the autoload.php config file; still having the same problem. Any ideas how to go about this?
Change
$this->load->database();
to
$this->load->library('database');
database is not a direct method. It is a library in codeigniter and you have to load it as a library.
You can also autoload database library in autoload.php.
UPDATE:
You are using the same name for your class and method. In PHP4, a method which has the same name as class name was treated as constructor, but if you are using codeigniter 2+, you have to use PHP5 constructor which is
function __construct()
{
parent::__construct();
/*Additional code which you want to run automatically in every function call */
}
You cannot give a method same name as class name in Codeigniter 2+. Change the method to anything else. You can name the method index if you want it to load by default.
This should solve your problem.
CodeIgniter User Guide, Creating Libraries Section:
To access CodeIgniter's native resources within your library use the
get_instance() function. This function returns the CodeIgniter super
object.
Normally from within your controller functions you will call any of
the available CodeIgniter functions using the $this construct.
$this, however, only works directly within your controllers, your
models, or your views. If you would like to use CodeIgniter's classes
from within your own custom classes you can do so as follows:
First, assign the CodeIgniter object to a variable:
$CI =& get_instance();
Once you've assigned the object to a variable, you'll use that
variable instead of $this:
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
etc.
Hope this helps. You could also put the $CI in a constructor.
Your code would look something like this:
class Tutorial
{
public $CI;
/**
* Constructor.
*/
public function __construct()
{
if (!isset($this->CI))
{
$this->CI =& get_instance();
}
$this->CI->load->database();
}
}

CodeIgniter issue with database library

I am trying to build a custom library of a Shopping Cart. I had some issues with loading the database library into the Cart class but I solved that with using:
$CI =$ get_instance();
$CI->load->database();
But now whenever I want to use the database library it gives me the error: "Trying to get property of non-object" on this line:
$this->CI->db->insert("carts", $data);
I should mention that I declared the $CI variable at the top of my class like this:
var $CI;
Any help would be appreciated!
Thanks in advance.
If $CI is a class variable, as you say it is, then you'll need to use
$this->CI =& get_instance();
$this->CI->load->database();
instead of
$CI =& get_instance();
$CI->load->database();
initialize $this->CI in constructor to make available in every methods.

Categories