Welcome Guest, Not a member yet? Register   Sign In
Use a static method from a personal library without instance
#1

[eluser]Davjack[/eluser]
Hello everybody,

I recently discovered IC .

During my learning of this framework, I wanted to use a static method from one of my personal libraries without success*.

* I want to use it without instance its library and if I understood, when I do
Code:
$this->load->library('mylibrary');
echo $this->mylibrary->myStaticMethod;
my class Mylibrary is needlessly instanced.

My question : Is that possible to use the static method (located in system/application/librairies/mylibrary.php) in a controller (e.g. located in system/application/controllers/mycontroller.php) without instancing the class Mylibrary ?
#2

[eluser]webthink[/eluser]
you could manually include the file and then call mylibrary::my_method() I think anything you load using CI's load automatically instances the class
#3

[eluser]nmweb[/eluser]
webthink's right, loading means instantiating. There should either be a flag in the load->library() method or a special load->require/include method so you can use static methods or use classes without having instance in the CI superobject.
#4

[eluser]webthink[/eluser]
that would be ideal. Unfortunately the loader class isn't one of those ones I would ever feel comfortable overloading but it definitely could be done. It does follow CI's philosophy of minimalism though so perhaps we can make a feature request.
#5

[eluser]Rick Jolly[/eluser]
What's wrong with include()/require()? Alternatively, you could put your library in the "helpers" folder. It'll get included but not instantiated.
#6

[eluser]wiredesignz[/eluser]
Rick is right, helper classes are loaded without instantiation and remain static, as do plugin classes.
#7

[eluser]webthink[/eluser]
Because it may make sense to have a library that functions as an instantiated object when necessary but also houses a number of functions that can be called statically when you don't need full instantiation. Not exactly out of the realms of possibility.
#8

[eluser]nmweb[/eluser]
Moreover, a load->include() method will take the library's name as argument not an exact filename. This means, in analogy with library(), it doesn't matter whether the file is in system/libraries or application/libraries. Same goes for MY_ extensions.

Often I find, it is not necessary to load a library into the superobject. Pagination or Calendaring class really do not need to be put in the superobject, of course they can but why enforce it. Right now you can only retrieve the CI object, why not make Session a singleton so when I build a library that only uses session stuff I have to load the CI object to retrieve it.
#9

[eluser]Davjack[/eluser]
Thank you all for your answers.
I think I will create an extension of the load library to includes some of the libraries without instantiation.
#10

[eluser]Davjack[/eluser]
I read through the loader library and I finally found that, that is possible to load an library without instance it simply with a "false" parameter :
$this->load->library('mylibrary',false);




Theme © iAndrew 2016 - Forum software by © MyBB