CodeIgniter Forums
Calling functions of other classes - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: Calling functions of other classes (/thread-4084.html)



Calling functions of other classes - El Forum - 11-06-2007

[eluser]Joozt[/eluser]
I am fairly new to the CondeIgniter framework.

If i read things right then it isn't possible to call a function of an other controllers in a controller. So if i have a class with functions that I want to use in Controllers where do I place it? Should I use it as model? helper? or what


Example:
Say I have a class called Lock witch checks if content is locked or not (in use by an other user.

Code:
class Lock extends ?????
{    
    /**
     * Class initialization
     */
    public function Lock()
    {
        parent::__construct();
        
        # Load path settings file
        $this->load->config('application', true);

        $this->unvalidated_dir = $this->config->item('unvalidated_dir','application');
        $this->archived_dir = $this->config->item('archived_dir','application');
        $this->converted_dir = $this->config->item('converted_dir','application');
        $this->archived_dir = $this->config->item('archived_dir','application');
        $this->thumb_dir = $this->config->item('thumb_dir','application');
        $this->usable_dir = $this->config->item('usable_dir','application');    
        $this->unvalidated_dir = $this->config->item('unvalidated_dir','application');
    }
    function index()
    {
        Lock::setLock("get_new.xml");
    }
    
    function checkLock($filename){
            $currenttime=time();
            $xmlData = file_get_contents($this->archived_dir.$filename);

                    $domDoc = new DOMDocument ();
                    $domDoc->preserveWhiteSpace = false;
                        if ($domDoc->loadXML ($xmlData))
                    {
                        //Set lock value of XML to 1
                        $nodeItems = $domDoc->getElementsByTagName("locked");    
                                                    
                        foreach ($nodeItems as $nodeItem)
                        {
                            $lock=$nodeItem->nodeValue;
                            
                        }
                        //END
                        
                        //Set lock_timestamp value to current time
                        $nodeItems = $domDoc->getElementsByTagName("locked_timestamp");    


                        foreach ($nodeItems as $nodeItem)
                        {
                            $lockTime=$nodeItem->nodeValue;
                        }
                        //END            
                    }
                    //ADD 20 min to the locktime
                    
                    
                    //Check if a file is locked
                    if($lock!=1){
                        return false;
                    }else{
                        //ADD 20 min to the locktime
                        $lockTime=$lockTime+(20*60);
                        if($currenttime>$lockTime){
                            return false;
                        }else{
                            return true;
                        }
                        
                    }
            
                      
        
    }


Now I have an other Controller class Content

Code:
<?php
class Content extends Controller {
    
    function Content(){
        
        parent::Controller();    
        
    }
    
    function index(){
          if(Lock::checkLock){
        $this->load->view('content', $page);
          }
    }
}
?>


So I want to use my Lock class in other controllers, but what is the best place to place this Class.

I am programming object oriented and want to keep things clean.

Thanks in advance


Calling functions of other classes - El Forum - 11-06-2007

[eluser]alexsancho[/eluser]
Put the class on "Libraries" dir and autoload it. In this way, class methods become available on controllers, models and other libraries too.


Calling functions of other classes - El Forum - 11-07-2007

[eluser]Joozt[/eluser]
I am now using the class as model. Is that a good idea or not?


Calling functions of other classes - El Forum - 11-07-2007

[eluser]xwero[/eluser]
It looks that you are getting data so it's in compliance with the MVC pattern, nobody states you only can use models to retrieve database data.


Calling functions of other classes - El Forum - 11-07-2007

[eluser]Joozt[/eluser]
I just read the hole user guide and it seems that is might be better to use it as Library, but can you load a config in a Library?

When I move my class to libray and try to load the config file in the constructor:

# Load path settings file
$this->load->config('application', true);

I get the following error:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Lock::$load

Filename: libraries/lock.php

Line Number: 11

Fatal error: Call to a member function config() on a non-object in D:\www\zoover\www\cuctema\application\libraries\lock.php on line 11


SO is it not possible to load a config in a library ? If not I wil just use it as model.


Calling functions of other classes - El Forum - 11-07-2007

[eluser]Joozt[/eluser]
Oh I forgot to say thanks for the replies that where already given!


Calling functions of other classes - El Forum - 11-07-2007

[eluser]xwero[/eluser]
You have to get the CI object to use loaded classes
Code:
$CI =& get_instance();



Calling functions of other classes - El Forum - 11-07-2007

[eluser]Joozt[/eluser]
Is there a good page that explains how to exacly build libraries? I read the userguide but it is a bit short.


Calling functions of other classes - El Forum - 11-07-2007

[eluser]xwero[/eluser]
There isn't much involvement as far as CI is concerned, that is why it's easy to make libraries you can find on the web to work with CI.

The only thing you have to be aware of is that if you want to use CI methods you have to use the get_instance function. If you are using CI methods in several of your methods it's best you assign the get_instance function to a variable.

With extending CI libraries you have to see how the CI methods are connected to the loaded libraries. For instance the language library uses in all the methods an own get_instance object.