• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is loading through static class is bad idea?

#11
(06-13-2017, 01:46 PM)kharota Wrote:
(06-13-2017, 12:38 PM)skunkbad Wrote:
(06-13-2017, 08: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

#12
(06-13-2017, 01:40 PM)kharota Wrote:
(06-13-2017, 12:20 PM)PaulD Wrote: Less code is not necessarily the best code (unless you are playing code golf).

Great code is beautiful because it is easily followed, clear, DRY, easily maintained, well thought out, robust and flexible.

Sir, if less code is same code why not use that one?

$this->load->view (has 17 chars)
Load::view (has 10 chars)

 how many more hours i have to do (for simple thing) if i have to go through all loading and other stuff? time is money and as freelancer its even more costly. 

I understand your point. are not we using frameworks to save our time?

https://github.com/nicolas-goudry/CI-PHP..._CI_CC.php
Works on netbeans too.

If you use a My_Controller and need only the loader you can add this line in the MY_Controller
PHP Code:
/**
* @property CI_Loader $load Loads framework components.
**/ 
https://phpdoc.org/docs/latest/reference...perty.html
Reply

#13
(06-13-2017, 01:40 PM)kharota Wrote:
(06-13-2017, 12:20 PM)PaulD Wrote: Less code is not necessarily the best code (unless you are playing code golf).

Great code is beautiful because it is easily followed, clear, DRY, easily maintained, well thought out, robust and flexible.

Sir, if less code is same code why not use that one?

$this->load->view (has 17 chars)
Load::view (has 10 chars)

 how many more hours i have to do (for simple thing) if i have to go through all loading and other stuff? time is money and as freelancer its even more costly. 

I understand your point. are not we using frameworks to save our time?

If you're worried about 7 characters of typing, why not:
L::v
That's 4 characters, 6 less than your example.

Just make sure the time you save typing out 7 less (or 13 less) characters will make the code maintainable, readable, understandable and will actually save money (cause you've got to explain to every freelancer, developer, etc that, instead of using the default standard, we're doing it this way, standards exist for a reason, but if 7 characters are going to break the bank, by all means, go for it).

Frameworks save time because developers understand how they work and do not need to figure out, for example, how to display a view in the controller, they all know it is $this->load->view and they know it will work as expected without complication and for them needing to roll their own code.
Reply

#14
(06-13-2017, 10:32 PM)Paradinight Wrote: https://github.com/nicolas-goudry/CI-PHP..._CI_CC.php
Works on netbeans too.

Indeed - very handy. Did not know it would work for Netbeans. Thanks.

Netbeans also offers "Code Templates" where you can define abbreviations that will expand to text you define. For instance, I set up an template where typing tlv[tab] expands to $this->load->view('view'); with view "selected" and ready for input. Enter the text and press enter and your cursor is at the end of the line.

I've also implemented 'tlm' and 'tll' for model and library respectively.

PHPStorm likely has a similar feature. Pretty sure Eclipse does but it's been a long time since I drove that app so I might be wrong.

There's always macros! Even Notepad++ does those.
Reply

#15
If you really want to save some typing create a helper.

/application/helpers/load_helper.php
PHP Code:
<?php
if(!function_exists('view'))
{

 
   function view($view$data null$return FALSE)
 
   {
 
       $obj = & get_instance();
 
       return $obj->load->view($view$data$return);
 
   }

}

if(!
function_exists('library'))
{

 
   function library($lib$config null$obj_name null)
 
   {
 
       $obj = & get_instance();
 
       return $obj->load->library($lib$config$obj_name);
 
   }

}

if(!
function_exists('model'))
{

 
   function model($model)
 
   {
 
       $obj = & get_instance();
 
       return $obj->load->model($model);
 
   }

}

if(!
function_exists('helper'))
{
 
   function helper($helper)
 
   {
 
       $obj = & get_instance();
 
       return $obj->load->helper($helper);
 
   }



Autoload this helper and you're down to typing

PHP Code:
view("welcome"); 
Reply

#16
(06-14-2017, 02:58 PM)dave friend Wrote: If you really want to save some typing create a helper.

/application/helpers/load_helper.php
PHP Code:
<?php
if(!function_exists('view'))
{

 
   function view($view$data null$return FALSE)
 
   {
 
       $obj = & get_instance();
 
       return $obj->load->view($view$data$return);
 
   }

}

if(!
function_exists('library'))
{

 
   function library($lib$config null$obj_name null)
 
   {
 
       $obj = & get_instance();
 
       return $obj->load->library($lib$config$obj_name);
 
   }

}

if(!
function_exists('model'))
{

 
   function model($model)
 
   {
 
       $obj = & get_instance();
 
       return $obj->load->model($model);
 
   }

}

if(!
function_exists('helper'))
{
 
   function helper($helper)
 
   {
 
       $obj = & get_instance();
 
       return $obj->load->helper($helper);
 
   }



Autoload this helper and you're down to typing

PHP Code:
view("welcome"); 

Its a good idea. Laravel and CI4 both using this. CI3 is lacking.
Reply

#17
(06-14-2017, 07:02 AM)Kaosweaver Wrote:
(06-13-2017, 01:40 PM)kharota Wrote:
(06-13-2017, 12:20 PM)PaulD Wrote: Less code is not necessarily the best code (unless you are playing code golf).

Great code is beautiful because it is easily followed, clear, DRY, easily maintained, well thought out, robust and flexible.

Sir, if less code is same code why not use that one?

$this->load->view (has 17 chars)
Load::view (has 10 chars)

 how many more hours i have to do (for simple thing) if i have to go through all loading and other stuff? time is money and as freelancer its even more costly. 

I understand your point. are not we using frameworks to save our time?

If you're worried about 7 characters of typing, why not:
L::v
That's 4 characters, 6 less than your example.

Just make sure the time you save typing out 7 less (or 13 less) characters will make the code maintainable, readable, understandable and will actually save money (cause you've got to explain to every freelancer, developer, etc that, instead of using the default standard, we're doing it this way, standards exist for a reason, but if 7 characters are going to break the bank, by all means, go for it).

Frameworks save time because developers understand how they work and do not need to figure out, for example, how to display a view in the controller, they all know it is $this->load->view and they know it will work as expected without complication and for them needing to roll their own code.

Sir, first of all L::v is unreadable. Load::view is readable and understandable. I dont know its good or bad thats why I am asking the question here.  Its not about just 10 Char and its not about just loading views. Its helps in autocomplete its easy to read and its cleaner in syntax thats the reason laravel is using such proxy classes to main classes.
Reply

#18
Personally, I think the use of static classes is fine, should not cause any load issues, should not slow down your application in any noticeable way.

So to answer your original question, IMHO, it is fine.

On the other issues raised, personally I still cannot see the point of it in the example you gave. The majority of my time by far is spent planning, problem solving for best UI experience and debugging (head scratching usually), not typing. That is not to say syntax is not important, but syntax is the least of my problems. The other big one, is sharing code with other developers. This normally means me taking the working prototype or first draft, and refactoring code to be cleaner, more easily maintained, clearer naming of functions, tables, etc. Often a hardcoded shortcut following a change in the UI means messy code, which is fine when you are first writing it, but 6 months later can become a nightmare.

I am getting better at doing a lot of this in the first build, but customers change their mind all the time. Often meaning a fundamental cornerstone of the build plan is changed. They just do not understand that an apparently little change can have a massive affect. Like "Actually, now we have thought about it, customers should have access to multiple accounts with different pricing plans".

However, static methods are not something I really employ, but following your post it has certainly made me rethink that. Perhaps I have overlooked how useful static classes can actually be. I will definitely be thinking about them in the next build.

Thank you.

Paul.
Reply

#19
If your arguments are all about how many characters you type, you're missing the forest for the trees.

Static methods introduce hard dependencies, making your codebase really hard to evolve - this is why they should be avoided, unless you need something to be completely stateless.
Reply

#20
(06-15-2017, 04:00 AM)PaulD Wrote: Personally, I think the use of static classes is fine, should not cause any load issues, should not slow down your application in any noticeable way.
Thank you, I got my answer.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.