Welcome Guest, Not a member yet? Register   Sign In
How to load a different library if the requested one fails to load?
#1

[eluser]ndriana[/eluser]
Hi,

I'm sorry if it has been asked before but I don't seem to find any answer around the web. I am trying to apply my object-oriented design knowledge on my code and I'm stuck on this one.

pseudocode
load libA, name it libUsed
if it fails{
load default lib, name it libUsed
}
use libUsed for the rest of the code

My problem is whenever it fails to load a library, codeigniter display an error message right away "Unable to load the requested class," instead of giving an opportunity to the developer to handle it.

Is there any setting or flag somewhere that I'm missing? All I can find is how to customize the error displayed.

ps: I tried the strandard approach
() ? :
try catch
#2

[eluser]Tpojka[/eluser]
Code:
if (!$this->load->library('libA'))
{
  $this->load->library('libB');
}
#3

[eluser]ndriana[/eluser]
Something like that is exactly what I'm looking for. But unfortunately, in your code, as soon as $this->load->library('libA') fails to load, codeigniter doesn't even go throught the "!" but displays a the error message already.
#4

[eluser]Tpojka[/eluser]
You can try something like this:
Code:
if (file_exists(APPPATH . 'libraries/My_class.php'))
{
  echo 'Library exists.'; // do what you want
}
else echo 'Library doesn\'t exists.';// or load existing class
But asure at least one class exists.

Probably there is even more efficient way, but I failed with function class_exists()...
#5

[eluser]CroNiX[/eluser]
Why would you not know if a library is installed?
#6

[eluser]ndriana[/eluser]
Thanks Tpojka, it perfectly works. Here is my code:
Code:
protected function render($request, $template=NULL, $data=NULL){
$device = ($request['device'])? $request['device']: 'desktop';
$templName['template']=($template)? $template : 'default';

(file_exists(APPPATH . 'libraries/views/s_'.$device.'_lib.php'))?
$this->load->library('views/s_'.$device.'_lib', $templName, 'viewLib'):
$this->load->library('views/s_default_lib', $templName, 'viewLib');

$this->viewLib->load($data);
}
It's a little scrumbled but I'll go with it till I have time.

CroNiX> you are right, I should know but I am used to work with C++ and you never know what's inside of a given compiled library. It's a principle to check and CI automatically does it but it doesn't hand back the flag.
Also, I have filtered this code to its bone but my goal is to know the user's device and target them accordingly. Not as the basic like desktop, tablet, smartphone but it can go deeper as Samsung Galaxy S4 smartphone. And my theme would be similar to the device's interface.
There are long libraries you can use from the internet that will pinpoint to you the device and those libaries are updated time to time. So if their outputs are some random device like Huawei W1 and I haven't had time to work on that or they are not numerous enough to be targeted, I'd like the server to automatically fall back to the smartphone default theme.
And lastly, it is also a principle when working in team. Some collegues are just slow.
#7

[eluser]CroNiX[/eluser]
That makes a bit more sense, however it might be faster and more efficient to create a single class containing all of the methods for various devices, and check if the method exists in the class which is loaded and already in memory rather than a file exists on the filesystem, since you'd be loading the class beforehand anyway.

Code:
$this->load->library('blah');
$device = ($request['device'])? $request['device']: 'desktop';
$method = (method_exists($this->blah, $device)) ? $device : 'default_for_unknown';
$this->blah->$method();





Theme © iAndrew 2016 - Forum software by © MyBB