Welcome Guest, Not a member yet? Register   Sign In
Am I loading a class twice?
#11

[eluser]Buso[/eluser]
I don't think it's a matter of style, but anyway here's the non-static version:

Code:
function profile_link($link_text='Profile'){

  if(get_instance()->user->is_logged_in()){
    echo anchor('user/profile', $link_text);
  }

}
#12

[eluser]nuwanda[/eluser]
Why isn't it a matter of style?

I understand that I can declare a reference to the user library in the helper and then use it as you suggest.

Maybe I'm missing something.
#13

[eluser]Buso[/eluser]
If a method wasn't declared static, and you call it statically, it may work.. or not. Or it may do something unexpected. Unless the method's docs says that it can be called statically, you shouldn't do that.
When a method is called statically, you are sending a message to the class, which knows nothing about the instance. If the method depends on an instance attribute to work, it will fail.

Suppose you have a class Car, and two instances of it, eg: a red car, and a yellow car.
And you call Car::get_color().. Which color should it return? It will fail, unless it was designed to be called statically.
#14

[eluser]nuwanda[/eluser]
Ah, ok. Yes.

So, just for interest, can you answer my previous question?

Does CI allow me to use a static call simply because the library exists in the library dir or because I've instantiated it previously in the controller?
#15

[eluser]Buso[/eluser]
Because the class has been loaded (not instantiated)

And it's php who lets you do that, not CI
#16

[eluser]nuwanda[/eluser]
Right.

So the load->library call makes the members of the class available without creating an instance.
#17

[eluser]Buso[/eluser]
You can choose if they are instantiated or not, there's an optional parameter I think.

What I ment is that once a class is loaded, you can call static methods, even if you haven't instantiated it.

In this case you are instantiating it.
#18

[eluser]nuwanda[/eluser]
Thanks, Buso.

Most appreciated.




Theme © iAndrew 2016 - Forum software by © MyBB