CodeIgniter Forums
Using custom library - best practice? - 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: Using custom library - best practice? (/showthread.php?tid=21898)

Pages: 1 2


Using custom library - best practice? - El Forum - 08-24-2009

[eluser]Flak[/eluser]
Well, I have a Slider library and a method to check what slide is loading, is it img or swf.


Code:
class Slider {
    function loadTypes($type,$path,$name)
    {
        switch($type)
        {
            case 'swf':
            $types = '[removed]http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js[removed]';
            $types = '[removed]var so = new SWFObject("'.base_url().'/'.$path.'", "'.$name.'", "690", "270", "8", "#336699");so.write("flashcontent");[removed]';
                break;
            case 'img':
            default:
                $types = '<img src="'.base_url().'/'.$path.'" alt="'.$name.'">';
                break;
        }
        return $types;
    }
}

I have load library on controller,
And I was trying to call this on View:

Code:
<div id="promobox">
    &lt;?  foreach($list->result() as $item): ?&gt;
    &lt;?   $this->Slider->loadTypes($item->type,$item->path,$item->name); ?&gt;
    &lt;? endforeach?&gt;
    </div>

Which won't work. I know that maybe this don't work, but any idea how to achieve this.

Thanks.


Using custom library - best practice? - El Forum - 08-24-2009

[eluser]pistolPete[/eluser]
You need to add an echo statement:

Code:
&lt;?php echo  $this->Slider->loadTypes($item->type,$item->path,$item->name); ?&gt;



Using custom library - best practice? - El Forum - 08-24-2009

[eluser]Flak[/eluser]
tried already but still same:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: CI_Loader::$Slider

Filename: views/home.php

Line Number: 94

Fatal error: Call to a member function loadTypes() on a non-object in D:\AppServ\www\ninja\system\application\views\home.php on line 94


Using custom library - best practice? - El Forum - 08-24-2009

[eluser]davidbehler[/eluser]
Have you actually loaded the library?
Code:
$this->load->library('Slider');
or add it to the library autoload array in config/autoload.php


Using custom library - best practice? - El Forum - 08-24-2009

[eluser]Flak[/eluser]
Damn, why this don't work:

$this->Slider->loadTypes

and this does

$this->slider->loadTypes

while I have class Slider.

Looks like I have to make all lower to not have such a problems in future.


Using custom library - best practice? - El Forum - 08-24-2009

[eluser]jedd[/eluser]
You didn't mention in the first post, or answered the most recent, on how you load the library.


Using custom library - best practice? - El Forum - 08-24-2009

[eluser]Flak[/eluser]
[quote author="jedd" date="1251137528"]You didn't mention in the first post, or answered the most recent, on how you load the library.[/quote]

FLAK
Quote:I have load library on controller,
And I was trying to call this on View:

Code:
$this->load->library('slider');

but even if I do

Code:
$this->load->library('Slider');

I still need to user $this->slider->func.

Why is this CaseSensitive, doesn't make sense to me.


Using custom library - best practice? - El Forum - 08-24-2009

[eluser]jedd[/eluser]
[quote author="Flak" date="1251137781"]
but even if I do

Code:
$this->load->library('Slider');

I still need to user $this->slider->func.
[/quote]


You're loading it with $this->load->library('Slider'), the class name is Slider - I can see those two things from the code you've provided - and it still doesn't work when you talk to ->Slider-> ...?

Yes, that is very odd.

Can you confirm the file is called Slider.php in your library directory?


Using custom library - best practice? - El Forum - 08-24-2009

[eluser]Flak[/eluser]
Nope its slider.php .

I don't understand which one is right.

->Slider-> is filename or class name.


Using custom library - best practice? - El Forum - 08-24-2009

[eluser]jedd[/eluser]
[quote author="Flak" date="1251142824"]Nope its slider.php .
[/quote]

Ahhh.

Quote:I don't understand which one is right.

Happily, we have a [url="http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html"]resource[/url] that answers such question.

Yes, some consistency appears to be missing, and that'd be a valid complaint - but the documentation is complete and concise (taken from the above link)


Naming Conventions
File names must be capitalized. For example: Myclass.php
Class declarations must be capitalized. For example: class Myclass
Class names and file names must match.