Error: undefined property $load - php

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();
}
}

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();

Can't use library functions inside a controller

I am trying to create my own library so i can handle custom areas in my application. i have a small library located at application/libraries which i called randomizer. it looks like this:
Class Randomizer {
public function __construct()
{
parent::_construct();
$CI =& get_instance();
$CI->load->library('session');
$CI->load->database();
}
function test_function($name)
{
return 'Hi dear' . $name . 'welcome back!';
}
}
In my Controller i tried to test out the simple test_function:
public function index()
{
echo($this->Randomizer->test_function('John'));
exit;
}
And I am getting the following error
Call to a member function test_function() on a non-object
There are couple of possible errors i can see. first you are not loading the library you created inside your controller. in case you don't use $this->load->library('randomizer'); right before you call the library funcion. if you are going to use the library all over the controller then load it via the controller __consturct. Also i guess you are not extending an existing Class so the parent::_construct() is not needed. make sure you understand why you are using a library and when you should use helpers. Read more about Codeigniter Libraries
Please look closely to the documentation. There are a few problems.
First of all it seems that you have either not loaded the library ($this->load->library('randomizer')), otherwise you would get a syntax error because your are calling parent::_construct(), instead of parent::__construct. And you do not have to call that function anyway, because your class doesn't have a parent (it doesn't extend a class...).
Furthermore, although you declare your class with a capital, the variable will be lowercase. So you should call $this->randomizer->test_function()

How to Access $this variable inside nested functions in codeigniter?

I have a controller for Nu soap WSDL Like:
class webservice extends CI_Controller
{
function index()
{
$this->load->library('encrypt');
$this->load->model('MWSDl');
//...
function buy($apicode)
{
if(!$this->MWSDl->check_gateway($apicode)) //Error occurred php Cannot find "$this" Variable
}
//...
$this->nusoap_server->service(file_get_contents("php://input"));
}
}
How to Access $this inside buy function?
I tried by global $this But Error occurred!
Error:
Fatal error: Using $this when not in object context in \controllers\webservice.php on line 9
You are going wrong about the whole concept. PHP is not Javascript.You shouldn't nest functions, specially not when using OOP frameworks. If you run function index twice, the second time you will probably get an error that function buy is already declared since first run of index will declare function buy.
I would declare them as class member functions / methods.
class Webservice extends CI_Controller {
function __construct()
{
parent::construct();
$this->load->library('encrypt');
$this->load->model('MWSDl');
}
function index()
{
// do something like
$apicode = 'xxxxxx';
$this->buy($apicode);
//or what ever else you need to do
}
function buy($apicode)
{
if(!$this->MWSDl->check_gateway($apicode)) {
$this->nusoap_server->service(file_get_contents("php://input"));
}
}
}
No need to use globals in codeigniter.
Let me know if this helps.
I understand your question. I also had the same problem with nusoap. When registering a service you have to make a function. Thus in CI you are creating it inside a class function which makes the service function is nested inside, and cannot be taken outside.
Why don't you try this below? I use it all the time before, with helper etc. It's simple and I have tried it and it works.
Put this inside your nested function:
$ci =& get_instance();
and the rest you have to replace $this with $ci
eq. $ci->some_model->some_function(); or $ci->some_var = 'something';
And it also work if you tried to call a db.
I hope this will help you.

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();
}
}

Using a custom library inside of a custom library in codeigniter?

the problem I am having is that I am trying to use a custom library inside of another custom library I made within codeigniter. They are both in the libraries folder and CodeIgniter tells me that I have to first load the instance of CI which I did...
class MyClass {
public function __construct()
{
$CI =& get_instance();
$CI->load->library("OtherClass");
}
Now inside of a function within this class I am trying to use my other library..
public function my_function()
{
$CI->otherclass->function_inside_this_class();
}
The error I am getting is
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: CI
Filename: libraries/MyClass.php
Line Number: 20
Is there something I am missing in terms of declaring the codeigniter instance itself?
Thank you!
Your CI variable scope is limited to the constructor function at the moment. You can make a class variable that has class scope and will be accessible via $this->variable in all functions within that class.
class MyClass {
private $_CI; // make a private class variable here.
public function __construct()
{
$this->_CI =& get_instance();
$this->_CI->load->library("OtherClass");
}
public function my_function()
{
$this->_CI->otherclass->function_inside_this_class();
}
I think you shouldn't use CI. Include the second_library.php and create a new class in first_library.php. Or isn't possible?

Categories