Posts: 9
Threads: 2
Joined: Nov 2014
Reputation:
0
hi
i use following class to load the views like
Load::view("welcome.php");
is it bad idea? or its fine? i am not sure for using lots of instances?
<?php
Class Load{
static function view($view, $data = null){
$obj =& get_instance();
return $obj->load->view($view, $data);
}
static function model($model){
$obj =& get_instance();
return $obj->load->model($model);
}
static function library($lib, $config = null, $obj_name = null){
$obj =& get_instance();
return $obj->load->library($lib, $config, $obj_name);
}
static function helper($helper){
$obj =& get_instance();
return $obj->load->helper($helper);
}
?>
thank
Posts: 477
Threads: 18
Joined: Oct 2014
Reputation:
21
What's the reason you want to do this?
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Posts: 1,298
Threads: 62
Joined: Oct 2014
Reputation:
86
PHP Code: function ohce( $str ){ echo $str; }
oche('Hello World of Spaghetti Code');
Posts: 9
Threads: 2
Joined: Nov 2014
Reputation:
0
06-13-2017, 07:13 AM
(06-12-2017, 01:01 PM)albertleao Wrote: What's the reason you want to do this?
two reasons
1. static classes are more editor friendly. For example its hard to autocomplete on editor when we have codeigniter. but static works nicely.
2. its more good looking :  and less codes to write.
$this->load->view("welcome.php");
vs
Load::view("welcome.php");
If we have a real big project we can save lots time.
Now the question is WHY NOT?
Posts: 9
Threads: 2
Joined: Nov 2014
Reputation:
0
06-13-2017, 07:15 AM
(This post was last modified: 06-13-2017, 07:26 AM by kharota.)
(06-12-2017, 01:06 PM)skunkbad Wrote: PHP Code: function ohce( $str ){ echo $str; }
oche('Hello World of Spaghetti Code');
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.
Posts: 1,062
Threads: 42
Joined: Mar 2015
Reputation:
73
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.
Posts: 1,298
Threads: 62
Joined: Oct 2014
Reputation:
86
(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() { if( is_null( self::$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 ); }
// --------------------------------------------------------------------
}
Posts: 1,020
Threads: 15
Joined: Jun 2015
Reputation:
50
(06-13-2017, 07:13 AM)kharota Wrote: Now the question is WHY NOT?
In order to reduce typing by a few characters to use the CI_Loader class you want to: - Add another layer of abstraction
- Make your code less DRY
Looks to be a bad bargain to me.
Posts: 9
Threads: 2
Joined: Nov 2014
Reputation:
0
06-13-2017, 12:40 PM
(This post was last modified: 06-13-2017, 12:41 PM by kharota.)
(06-13-2017, 11:20 AM)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?
Posts: 9
Threads: 2
Joined: Nov 2014
Reputation:
0
06-13-2017, 12:46 PM
(This post was last modified: 06-13-2017, 01:38 PM by kharota.)
(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() { if( is_null( self::$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
|