codeigniter library within library issue - php

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

Related

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.

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

Instantiation of a PHP class without assignment to a variable

I came a cross this line of code in Codeigniter HMVC extension (by Wiredesignz), where a class got instantiated without getting assigned to a variable (class CI in Base.php)
The code :
class CI extends CI_Controller
{
public static $APP;
public function __construct() {
/* assign the application instance */
self::$APP = $this;
global $LANG, $CFG;
/* re-assign language and config for modules */
if ( ! is_a($LANG, 'MX_Lang')) $LANG = new MX_Lang;
if ( ! is_a($CFG, 'MX_Config')) $CFG = new MX_Config;
parent::__construct();
}
}
/* create the application object */
new CI;
What's the name of this technique?
What's the implication?
This has not a name and the implication is, that the constructor is definitely doing too much. The reason one wants to create an instance of a class without referencing it is, that he only wants the constructor wants to be executed, but nothing more. This means, that the constructor "does" something, but a constructor should only ensure, that an object is in a stable/valid state and nothing more.
In short: Don't assume that this is a good practice. The global and self::$APP = $this confirms my opinion that this is a bad piece of code.
I guess this could be seen as some sort of facade design.
-The Class(constructor is called) and assignment is done, albeit in the constructor itself.
so new CI is just extending the Super object and initializing its own constructor.
Similar to a function, function somefun(){ return }; somefunc();//call somefunc
CI_Controller loads all of the classes required to run Codeigniter, it is the SUPER object
$ci = &get_instance() // CI_Controller

Private static variables in php class

I have a few classes that are often run through var_dump or print_r.
Inside these classes I have a few variables that are references to other, rather large objects that only ever has one instance of each and are only used inside the classes (outside the classes have their own reference to these classes) I do not wish these classes printed in the output, so I have declared them as private static which is working fine.
But my IDE (PHPstorm) is flicking up an error-level alert with Member has private access when I access them through self::$ci->...
I am wondering if this is a bug in the IDE, highlighting because it's probably a bug (aka it's static but nothing outside the class can access it, why would you want to to do that?), or because there is actually something syntactically wrong with it?
As an example here is part of the class,
Note that =& get_instance(); returns a reference to the Code Igniter super object
private static $ci = null;
public function __construct(){
self::$ci = self::$ci =& get_instance();
}
public function product() {
if ($this->product == null) {
self::$ci->products->around($this->relative_date);
$this->product = self::$ci->products->get($this->product_id);
}
return $this->product;
}
In your product() method you're trying to access the private member self::$ci. Your IDE thinks that this method can be accessed anywhere, and detects a conflict with the private static member $ci.

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