Welcome Guest, Not a member yet? Register   Sign In
is loading through static class is bad idea?
#11

(06-13-2017, 12:46 PM)kharota Wrote:
(06-13-2017, 11:38 AM)skunkbad Wrote:
(06-13-2017, 07:15 AM)kharota Wrote: I understand but why not use less code? if we have big project? Are not they doing same thing in laravel? Please have a look at laravel facades.

It sounds like you have your reasons. I've used Laravel a few times, but I prefer CodeIgniter. I've been guilty of doing things my way for as long as I've been coding, and don't see any reason why you shouldn't do things your way. If you like facades, then go for it .... but at least do it a little cleaner. How about this:


PHP Code:
<?php

class Load {

    private static $CI NULL;

    // --------------------------------------------------------------------

    public static function _instance()
    {
        ifis_nullself::$CI ) )
            self::$CI =& get_instance();

        return self::$CI;
    }

    // --------------------------------------------------------------------

    public static function __callStatic$method$params )
    {
        $obj self::_instance();

        return call_user_func_array( [ $obj->load$method ], $params );
    }

    // --------------------------------------------------------------------



Thank you, Its perfect code. I am asking little different thing and that is =& get_instance()
doesnt create new instance all the time. Therefore it should not slower the framework right?

Second thing is still autocomplete doesnt work in editors because editor dont compile the codes instead they use created functions in class. Any solution for that?

I have edited my code with your codes



PHP Code:
class Load
  
{
 
   
    private 
static $CI NULL;
 
   
    
// --------------------------------------------------------------------
 
   
    public 
static function _instance()
 
   {
 
     if(is_null(self::$CI))
 
       self::$CI =& get_instance();
 
     
      return self
::$CI;
 
   }
 
   
    
// --------------------------------------------------------------------
 
   
    static 
function view($view$data NULL)
 
   {
 
     $obj self::_instance();
 
     
      return $obj
->load->view($view$data);
 
   }
 
   
    
// ---------------------------------------------------------------------
 
   
    static 
function model($model)
 
   {
 
     $obj self::_instance();
 
     
      return $obj
->load->model($model);
 
   }
 
   
    
// ---------------------------------------------------------------------
 
   
    static 
function library($lib$config NULL$obj_name NULL)
 
   {
 
     $obj self::_instance();
 
     
      return $obj
->load->library($lib$config$obj_name);
 
   }
 
   
    
// ---------------------------------------------------------------------
 
   
    static 
function helper($helper)
 
   {
 
     $obj self::_instance();
 
     
      return $obj
->load->helper($helper);
 
   }
 
   
    
// --------------------------------------------------------------------
 
 }
 
 //EOC
 
  

Your comments please

I don't have any solution for that, but you should just use __callStatic(). If you need autocomplete for CI Loader methods, you might need to re-think your career choice. I'm just half joking here, but seriously once you get solid experience with CI, you'll never forget how to load things.
Reply


Messages In This Thread
RE: is loading through static class is bad idea? - by skunkbad - 06-13-2017, 02:38 PM



Theme © iAndrew 2016 - Forum software by © MyBB