Welcome Guest, Not a member yet? Register   Sign In
How to avoid class name collisions in application/libraries classes?
#1

[eluser]sherbo[/eluser]
Hey All,

I have a bunch of classes in /system/application/libraries with various interdependencies. At the top of each file I have a bunch of
Code:
require_once
statements to specify the dependencies. All this works fine.

I'm trying to call a custom /system/application/libraries/Session_Management class function once during startup. I did this by adding the following to BASEPATH/index.php:

Code:
/** SET UP OUT CUSTOM SESSION MANAGER ****************************************/
require_once APPPATH.'/libraries/Session_Manager.php';

$sessionMgr = new Session_Manager();
$sessionMgr->init();

When I do this, however, I get the following error:
Code:
Fatal error: Cannot redeclare class ... some_class_in_/system/application/libraries

Is there a better way to call my /system/application/libraries/Session_Manager class?

What's the best practice for specifying library dependencies in CI and PHP in general?

I'm new to PHP and can't decide what's the best between include, require, require_once, and _autoload(). Class name collisions seem possible in each.

cheers,
Sherban
#2

[eluser]sherbo[/eluser]
BTW, I Googled this ...

---------------------------------------------------------------
From fastest to slowest:

Code:
include
require
include_once
require_once

Why?

Code:
include
just brings the file in.

Code:
require
first stats the file and then brings the file in

Code:
include_once
has to go through the entire loaded files table to ensure the file hasn't been included, then it brings it in.

Code:
require_once
first stats the file, then looks through the entire loaded files table to ensure the file hasn't been included, then brings it in.

Does this mean I should always use require()?

Isn't require_once() more memory-efficient?
#3

[eluser]xwero[/eluser]
The CI way is to load the library using
Code:
$this->load->library('Session_Manager');
or add it to your autoload libraries array.

I see it uses an init the best way to deal with that is to call/move the init in your constructor if you want to autoload your library. If you load the library you have the possibility to add params to using the second argument of the load->library method. But then your constructor needs to accept an argument too
Passing Parameters When Initializing Your Class
#4

[eluser]sherbo[/eluser]
The CI term for "loading a library" is misleading. It's instantiating a class, not "loading" it for possible reference. Also, it forces your library to share a common constructor signature.

What if your library has only static methods? Why would you need to "load" (aka instantiate) it?

There's no way to reference a CI library w/o instantiating it apart from using "require" statements.

To me, CI's library "loading" is very inelegant.
#5

[eluser]xwero[/eluser]
why don't you need to instantiate a class with only static methods?
#6

[eluser]sherbo[/eluser]
How would I reference a a custom library class in the /application/libraries/ directory without doing this?

Code:
$this->load->library('Session_Manager');
$this->session_manager->some_static_function();


The only other alternative is:

Code:
require_once 'Session_Manager';
Session_Manager::some_static_function();




Theme © iAndrew 2016 - Forum software by © MyBB