CodeIgniter Error using Memcached inside cURL - php

I'm getting an error when trying to use memcached inside a cURl write function inside Codeigniter.
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $str)
{ //print(htmlentities($str)."\n\n");
$ci &= get_instance();
$ci->cache->memcached->save('foo', 'bar', 600); return strlen($str);
});
The errors are
Message: Undefined variable: ci
Message: Object of class admin_test could not be converted to int
Any suggestions?
I tried the advice from this link CodeIgniter: can't access $this within function in view
and from here:
Object of class stdClass could not be converted to string
I can use the $ci syntax to make a database query like this
$ci->db->query('SELECT * FROM TABLE');
but not with memcached.

I figured it out, not nearly as hard as I thought.
I was using
$ci &= get_instance();
But what I needed was
$ci = get_instance();

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 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.

MySQL insert inside library with codeigniter

I'm trying to insert a number into the database from within a library using,
$data = array(
'id' => $id,
'so' => '1',
'username' => $username
);
$this->db->insert('db', $data);
but whenever I run it, I get "Fatal error: Call to a member function insert() on a non-object in"
Anyone know why?
Thanks
If you're doing this inside a library, you can't reference the $this object like you normally do in a model. Instead, you'll need to load an instance of CI like so:
$CI =& get_instance();
Then you should be able to insert a record into the database like this:
$CI->db->insert('db', $data);
Don't forget to load the database library as well if it's not in autoload.
In a library "$this" refers to the class that you are coding (OOP concepts).
The DB object is part of the CI superobject. So in order to use db in your class you will have get a reference to the CI object like this
$CI =& get_instance();
And then rewrite your insert statments like this
$CI->db->insert('db', $data);

Categories