CodeIgniter Forums
how to call a function of another class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: how to call a function of another class (/showthread.php?tid=34542)

Pages: 1 2


how to call a function of another class - El Forum - 10-02-2010

[eluser]Sumon[/eluser]
here are two simple classes
Code:
class A extends Controller {
public function FA1()
{
   // need to call B class FB1 function. how ?
}
}
class B extends Controller {
public function FB1()
{
   // some statements
}
}

how could i call FB1() from FA1() ?
Thanks in advance.


how to call a function of another class - El Forum - 10-02-2010

[eluser]echoDreamz[/eluser]
Rethink your design. Controller methods are created and assigned for that specific controller. If you need to share them between controllers (I have methods I use on many different controllers) extend it twice like so... (or you can use libraries / helpers).

Code:
class SharedController extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function SharedMethod()
    {
        
    }
}

class TopController extends SharedController
{
    function __construct()
    {
        parent::__construct();
    }
    
    function ControllerMethod()
    {
        $this->SharedMethod();
    }
}



how to call a function of another class - El Forum - 10-02-2010

[eluser]dstegelman[/eluser]
Why are you calling a function from a controller inside of a controller. I can't think of many situations where you need to do this. if you need commonly shared data, you should probably be using a model to get it instead.


how to call a function of another class - El Forum - 10-02-2010

[eluser]echoDreamz[/eluser]
I use it simply because models are for database / low level stuff. My "middle man" controller is for things like javascript, css, other HTML stuff, creating templates / sections etc.

EDIT: However I am not calling another controller... I am simply calling methods from within the controller I extended.


how to call a function of another class - El Forum - 10-02-2010

[eluser]Dennis Rasmussen[/eluser]
Listen to the guys giving you help.
If you need to run some code in more than one controller then write the code in a library or even a model for reuse.
You don't call methods from another controller, that kinda makes no sense.


how to call a function of another class - El Forum - 10-03-2010

[eluser]Sumon[/eluser]
[quote author="echoDreamz" date="1286075352"]
Code:
class SharedController extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function SharedMethod()
    {
        
    }
}

class TopController extends SharedController
{
    function __construct()
    {
        parent::__construct();
    }
    
    function ControllerMethod()
    {
        $this->SharedMethod();
    }
}
[/quote]
Fatal error: Class 'SharedController' not found in D:\xampplite\htdocs\shopno-dinga-dsebd\application\controllers\sharedController.php on line 3
Please note that, sharedcontroller and topcontrollers are 2 separate files


how to call a function of another class - El Forum - 10-03-2010

[eluser]echoDreamz[/eluser]
This is because CI will only load 1 controller. Not both. So you need to manually include the file that houses shared controller. I went around this by using the PHP5 autoload method.

Code:
/**
* Overrides the default PHP class loader method
* Auto loads any classes that are not already within PHP
* For CI it provides a way to load classes that do not follow the subclass_prefix config value
*
* As a side-note this ONLY works on PHP v5, PHP v4 has poor object handling and does not allow
* autoloading of class objects
*
* @access Public
* @param String $class Class name
*/
function __autoload($class)
{
    static $objects = array();
    
    // Make sure this class has not already been loaded or is not a CI base class
    if (preg_match('{^CI_}', $class) || in_array($class, $objects))
    {
        log_message('debug', "Hooks: Skipped auto loading class {$class}");
        return;
    }
    
    // Attempt to load the requested class
    $class_path = APPPATH . "libraries/{$class}" . EXT;
    if (file_exists($class_path))
    {
        include($class_path);
        $objects[] = $class;
        log_message('debug', "Hooks: Autoloaded Class {$class}.");
    }
    else
    {
        log_message('error', "Hooks: Failed auto loading class {$class} from path {$class_path}.");
    }
}

This allows me to use libraries that do not follow the CI scheme, or have multiple levels of controllers.


how to call a function of another class - El Forum - 10-03-2010

[eluser]InsiteFX[/eluser]
This is what a MY_Controller is for! you should really listen to
what users explain to you in the forums.

InsiteFX


how to call a function of another class - El Forum - 10-03-2010

[eluser]echoDreamz[/eluser]
[quote author="InsiteFX" date="1286186780"]This is what a MY_Controller is for! you should really listen to
what users explain to you in the forums.

InsiteFX[/quote]

MY_Controller only works when you have your prefix set to MY_. We have different prefixes for our files vs customer specific files libraries / helpers. So for instance you have the prefix set to Customer_ and you try and use MY_Controller it will not work and will error saying class not found. This is why I use the autoloader above.

But yes, for those who use single prefixed libraries etc, it will work fine.

[quote author="Sumon" date="1286185390"][quote author="echoDreamz" date="1286075352"]
Code:
class MY_SharedController extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function SharedMethod()
    {
        
    }
}

class TopController extends MY_SharedController
{
    function __construct()
    {
        parent::__construct();
    }
    
    function ControllerMethod()
    {
        $this->SharedMethod();
    }
}
[/quote]
Fatal error: Class 'SharedController' not found in D:\xampplite\htdocs\shopno-dinga-dsebd\application\controllers\sharedController.php on line 3
Please note that, sharedcontroller and topcontrollers are 2 separate files[/quote]

The edited code above will work.


how to call a function of another class - El Forum - 10-03-2010

[eluser]InsiteFX[/eluser]
You can change the MY_ prefix to what ever you like!
It doe's not have to be MY_

InsiteFX