Welcome Guest, Not a member yet? Register   Sign In
extending native libraries
#1

[eluser]sanjoy[/eluser]
I am facing some problem when trying to extend native library. I have followed all the instruction from http://ellislab.com/codeigniter/user-gui...aries.html , but failure. Please suggest. Thanks in advance for your co-operation.
#2

[eluser]davidbehler[/eluser]
What exactly doesn't work? Describing your problem in more detail might make it easier for others to actually help you.
#3

[eluser]sanjoy[/eluser]
Actually I want to overwrite function output(). I have created a class Hello.php under application/libraries/ as follows

<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
class Hello{
function CI_Hello(){
$this->obj=&get;_instance();
}
function output(){
return 'Hello World';
}
}
?>


I have created another file MY_Hello.php under application/libraries/ as follows:-

<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Hello extends CI_Hello{
function My_Hello(){
parent::CI_Hello();
}
function output(){
return 'Overwrite Hello World string generated.';
}
}
?>

Now I want to overwrite the output() function from controller using [ basically I want output like 'Overwrite Hello World string generated.' ] the following code:-

$this->load->library('hello');
$this->hello->output();

Note: I have set $config['subclass_prefix'] on application/config/config.php with $config['subclass_prefix'] = 'MY_';


What's wrong with me. Please suggest.
#4

[eluser]WanWizard[/eluser]
Don't understand what you're trying to do here.

There is no CI library called Hello, so trying to extend CI_Hello is not going to work...
#5

[eluser]sanjoy[/eluser]
I want to overwrite the function output().... Following also will not work.

<?php
//Hello.php
if(!defined(‘BASEPATH’)) exit(‘No direct script access allowed’);
class Hello{
function Hello(){
$this->obj=&get;_instance();
}
function output(){
return ‘Hello World’;
}
}
?>




<?php
//MY_Hello.php
if(!defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

class MY_Hello extends Hello{
function My_Hello(){
parent::Hello();
}
function output(){
return ‘Overwrite Hello World string generated.’;
}
}
?>
#6

[eluser]sanjoy[/eluser]
Leave it. It Solved. Smile

Thanks everybody for your co-operation.
#7

[eluser]davidbehler[/eluser]
1. Please use the [_code][/_code] tag (remove the underscroes, I only had to add them to stop the forum from parsing them)

2. You want to overwrite the _output function of the controller class, right? Here you go:
Create a MY_Controller.php in your application/libraries folder
Code:
<?php
if(!defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

class MY_Controller extends Controller{
  function My_Controller(){
      parent::Controller();
  }
  function _output(){
      return ‘Overwrite Hello World string generated.’;
  }
}
?>
And now every controller file you have in application/controllers that should use this new _output method instead of the standard one must extend the new MY_Controller class. For example:
Code:
<?php
class Blog extends MY_Controller {

}
?>

Hope this helps




Theme © iAndrew 2016 - Forum software by © MyBB