CodeIgniter Forums
__get in libraries - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: __get in libraries (/showthread.php?tid=505)



__get in libraries - kylevorster - 12-11-2014

Hey,

I've read a tutorial somewhere to use the following

PHP Code:
private function __get($var)
{
 
 return get_instance()->$var;


rather than having to do this

PHP Code:
function __construct()
{
 
 $this->_ci = &get_instance();


Do that you can access other libraries, helpers and config files in the same way as controllers and you don't have to use $this->ci to acces them.

Would this method be correct, bad programming or just plain wrong ?


RE: __get in libraries - Rufnex - 12-11-2014

As test, make a print_r(get_instance()) and you will get the complete CI instance to access. So it depends on your codedesign and what you will do.

The first example i thik is from class with special needs. __get() is invoked when the method or property you want to use is inaccessible.

If you develop a library you should go with the second way but hey .. thats CI you can choose the way you like.


RE: __get in libraries - mwhitney - 12-15-2014

If you look at /core/Model.php, you'll see very similar code:

PHP Code:
public function __get($key)
{
    return 
get_instance()->$key;


Of course, whether this is a good idea for your library really depends on what you are doing in your library and how you expect it to be used. If you look through CI's libraries, you'll find that it, or something like it, is used in some libraries, but not others. In many cases, there are additional checks within the method to ensure different things occur based on what is being requested.


RE: __get in libraries - Narf - 12-16-2014

(12-15-2014, 10:11 AM)mwhitney Wrote: If you look at /core/Model.php, you'll see very similar code:


PHP Code:
public function __get($key)
{
 
   return get_instance()->$key;


Of course, whether this is a good idea for your library really depends on what you are doing in your library and how you expect it to be used. If you look through CI's libraries, you'll find that it, or something like it, is used in some libraries, but not others. In many cases, there are additional checks within the method to ensure different things occur based on what is being requested.

CI_Model is extended by developers using CI, so that's a convenience method for them - it would be a pain in the ass not to have it.

In a library however, the author writes it once and everybody else just calls the class methods, so that shortcut doesn't make much sense - only the author would benefit.

The bottom line is, it's up to the author ... if you're a lazy coder and fewer characters is all you care about, then you'll probably go for it. If you care about performance and code that's more expressive and makes more sense, then you wouldn't.


RE: __get in libraries - Hobbes - 12-20-2014

well if your talking about accessing ci super object (for a lack of better term) form a class file in the libraries folder then the standard practice and imho best and easiest way to do it is like this:

Code:
class Someclass
{
 private $ci;

 function __construct()
 {
     $this->ci = get_instance();      
 }

 function do_something()
 {
     return $this->ci->config->item('uri_protocol');
 }
}