Welcome Guest, Not a member yet? Register   Sign In
User Created Library - Best Practices
#1

[eluser]cartalot[/eluser]
looking for any suggestions, advice, etc on creating Libraries.
I know there are lots of ways of doing things - just looking for some input and best practices.
and if anyone has any suggested user CI libraries to look at

* what are best practices for returning from / getting data out of the Library -
for example i have a method in a library that gets a couple different db results
i'm assigning them to $data and returning $data

Code:
$data['cartitems']  = $this->_CI->phcartmodel->getCartItems($uid);
$data['cartotal']  = $this->_CI->phcartmodel->getCartfull($cid);
return $data;

it works but is this a mistake because $data is used in the controller?

* related - is it better to always return a value that can be checked -
or are there times when RETURN FALSE is enough?

* for development i am using something like this to keep track of the code and see where things are breaking
Code:
$this->devmessage .= 'Start update cart <br>';

and then outputting that in view -- which works fine across methods in the controller, but awkward for in and out of library.
suggestions?

* what is suggested best practice for declaring
Code:
$CI =& get_instance();
when i do this inside each method in the library it works fine.
but when i put it in the library constructor so i don't have to repeat it -- it does not work.

however when i use this version in the library constructor (and for example load the session class)
Code:
$this->_CI =& get_instance();
$this->_CI->load->library('session');

then i can use
Code:
$this->_CI->session->userdata('uid')
in any method in the library - which is fine but just wondering why.

#2

[eluser]PhilTem[/eluser]
* You should always return the data the way you need them. If however you want others to use these methods too then you'd probably think about a more general value returning. Maybe it's better to remove the string-keys and make it a numeric array since you can then use
Code:
list($cart_items, $cart_total) = $this->your_library->the_method();
That's at least what I prefer.

* I tend to return TRUE/FALSE on methods that are like void-methods to check if the actions ran successfully. If my library gets some data, I usually return this data. If it's not found - e.g. from a db-query - I return an empty array. That way I don't have to make type-checks, too (the thing with the third operator on comparison like === or !=== )

* Do you know
Code:
log_message($level, $message);
?
It's much easier and cleaner for debugging. And since it's config sensitive (depends on the log_treshold) it will only be run if you set the level accordingly.
log_message() is used in almost any library I have seen that people have written.
* For me putting
Code:
$this->CI =& get_instance();
works fine in the controller. However, I heard that if you autoloaded your library the constructor isn't run so you can't access $this->CI since it's not set
#3

[eluser]Samus[/eluser]
[quote author="cartalot" date="1333589520"]looking for any suggestions, advice, etc on creating Libraries.
I know there are lots of ways of doing things - just looking for some input and best practices.
and if anyone has any suggested user CI libraries to look at

* what are best practices for returning from / getting data out of the Library -
for example i have a method in a library that gets a couple different db results
i'm assigning them to $data and returning $data

Code:
$data['cartitems']  = $this->_CI->phcartmodel->getCartItems($uid);
$data['cartotal']  = $this->_CI->phcartmodel->getCartfull($cid);
return $data;

it works but is this a mistake because $data is used in the controller?
[/quote]

returning it as $data doesn't cause any conflicts, because you can then call the array indexes as anything you want in your controller.

e.g

Code:
$result = $this->library->function();
$cartitems = $result['cartitems'];
$cartotal = $result['cartotal'];

[quote author="cartalot" date="1333589520"]
* what is suggested best practice for declaring
Code:
$CI =& get_instance();
when i do this inside each method in the library it works fine.
but when i put it in the library constructor so i don't have to repeat it -- it does not work.

however when i use this version in the library constructor (and for example load the session class)
Code:
$this->_CI =& get_instance();
$this->_CI->load->library('session');

then i can use
Code:
$this->_CI->session->userdata('uid')
in any method in the library - which is fine but just wondering why.

[/quote]

Because '$CI' is treated as a variable, while $this->_CI refers to the actual class property.
#4

[eluser]Samus[/eluser]
[quote author="PhilTem" date="1333624214"]
* For me putting
Code:
$this->CI =& get_instance();
works fine in the controller. However, I heard that if you autoloaded your library the constructor isn't run so you can't access $this->CI since it's not set[/quote]
not true..
#5

[eluser]PhilTem[/eluser]
[quote author="Samus" date="1333626770"][quote author="PhilTem" date="1333624214"]
[...]since it's not set[/quote]
not true..[/quote]

Explain a little more. But it should be constructor instead of controller
#6

[eluser]cartalot[/eluser]
Thanks for replies so far! Here are some replies questions --
(again i understand there are many different ways to do the same thing, just looking for input)

Philtem wrote:
* Do you know

Code:
log_message($level, $message);
?
It’s much easier and cleaner for debugging. And since it’s config sensitive (depends on the log_treshold) it will only be run if you set the level accordingly.

Thanks yes i've used it to write to a log file. Actually i'm looking for something custom that would pass from controller to library and back to controller and then output directly to the view -- and would only have custom messages (not telling me whether models have loaded etc etc)

++ ++ ++ ++ ++ ++ ++ ++

Samus wrote:
returning it as $data doesn’t cause any conflicts, because you can then call the array indexes as anything you want in your controller.

e.g

Code:
$result = $this->library->function();
$cartitems = $result['cartitems'];
$cartotal = $result['cartotal'];

Thanks thats very helpful. I'm still learning the different ways of returning and then using the data.
Like the difference between when you would use
Code:
$cartitems
$this->cartitems
$data['cartitems']

++ ++ ++ ++ ++ ++ ++ ++

Philtem wrote:
If my library gets some data, I usually return this data. If it’s not found - e.g. from a db-query - I return an empty array. That way I don’t have to make type-checks, too (the thing with the third operator on comparison like === or !=== )

So if you do a db check and it returns zero -- Wouldn't it be better to return false, or even better return something specific like
Code:
$data['db_results'] = 'no records';
return $data;

which would then give a way to also check for and return other possible errors?

#7

[eluser]Samus[/eluser]
[quote author="PhilTem" date="1333638980"][quote author="Samus" date="1333626770"][quote author="PhilTem" date="1333624214"]
[...]since it's not set[/quote]
not true..[/quote]

Explain a little more. But it should be constructor instead of controller[/quote]
However, I heard that if you autoloaded your library the constructor isn’t run so you can’t access $this->CI since it’s not set

^This isn't true.




Theme © iAndrew 2016 - Forum software by © MyBB