Welcome Guest, Not a member yet? Register   Sign In
How can this not work??? help!
#1

[eluser]Luke D.[/eluser]
Hi all

I'm new to Codeigniter but not to php, esp. php5. Whatever. I've got my codeigniter system all set up brand new with practically no changes - nothing significant, and am working on a tiny little project. I want to build a class in the library that will make a navigation menu. I'm using the url helper. Here is the code - I pulled all the complex stuff out of it, and this won't work It is in the application library folder. Problem is it isn't correctly loading the url helper I think. Any ideas? This is making me feel retarded.

Luke


class Menumaker {

function Menumaker()
{
$obj =& get_instance();
$obj->load->helper('url');
/********************************
this didn't work either:
$this->load->helper('url');
********************************/
}

function make_menu($thing, $thing2)
{
return anchor($thing, $thing2);
/********************************
this didn't work either:
return $obj->anchor($thing, $thing2);
********************************/
}
}

$test = new Menumaker();
echo $test->make_menu('please', 'work');
#2

[eluser]Armchair Samurai[/eluser]
Try instantiating your library in your controller:

Code:
function Foo()
{
    $this->load->library('menumaker');
    echo $this->menumaker->make_menu('please', 'work');
}
#3

[eluser]Colin Williams[/eluser]
How exactly isn't it working? Fatal error, something else?

And Samurai, it wouldn't matter how it is instantiated, because the crux of the issue is (well, might be) getting the CI super-object with get_instance(), which is just a function.
#4

[eluser]Sarfaraz Momin[/eluser]
I did this by doing the following. I donno weather it would help because the code you have look ok to me.

Code:
class libraryname {
var $CI;
function libraryname()
{
$this->CI = &get;_instance();
$this->CI->load->database();
}

Hope it helps.

Have a good day !!!
#5

[eluser]Pascal Kriete[/eluser]
It's a very simple scope problem. $obj is a local variable, and thus only defined in the scope of the constructor. What you want is a class variable, so Sarfaraz's solution is what you're looking for.

[Edit: Idiotic code removed, ignore this post]
#6

[eluser]Colin Williams[/eluser]
Inparo, it's a helper, not a library. As far as I know, there is no such thing as $CI->anchor(), but there is anchor() in system/helpers/url_helper.php
#7

[eluser]Sarfaraz Momin[/eluser]
Colin: In case if it is a helper he can load the helper and call the function and it should work fine. I do not see any possible issues with it.
#8

[eluser]Colin Williams[/eluser]
Precisely, Sarfaraz. We really need to know how it's "not working". If he's getting a fatal error, it means the helper is not being loaded properly for some odd reason. If the anchor() function is just not returning what he expects, he might be using it wrong.

Try autoloading the helper, Louis, and see if it works then. Plus, please tells us how it isn't working. What happens?
#9

[eluser]Pascal Kriete[/eluser]
::headdesk::
Reading comprehension fail.

Totally didn't look at what the code actually did.
#10

[eluser]Colin Williams[/eluser]
Yeah, I just looked back up, inparo, and he does try $obj->helper(), so I see where you got that from Smile Heck, that might even be why it's not working for him :ohh:




Theme © iAndrew 2016 - Forum software by © MyBB