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.
Related
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();
$this->CI has $CI ($this->CI->CI)
And $this->CI->CI has $CI ($this->CI->CI->CI)
And $this->CI->CI->CI has $CI ($this->CI->CI->CI->CI)
And $this->CI->CI->CI->CI has $CI ($this->CI->CI->CI->CI->CI)
.....
......
$CI($this->CI->CI) has same keys and values with $this->CI (See following screenshot)
Image description
I can't determine how many $this->CI had $CI.
Why does $this->CI has $CI continuously?
Please tell me what the cause, what did I do something wrong?
There is no problem. $CI and $this are essentially the same memory location. The variables listing recusively lists the same memory structure again and again. The only way you will run out of memory is if you keep drilling down into the list and the debugger uses it all. In other words, what you a seeing is an illusion.
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
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();
}
}
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();
}
}