Welcome Guest, Not a member yet? Register   Sign In
Why can't we use static objects?
#3

(This post was last modified: 07-04-2015, 09:30 PM by skunkbad.)

There are a couple of issues preventing you from using the Input class as static.

1) There already exists an input class that is a concrete class, and it has not been namespaced, effectively "hogging" the name "Input".

2) You can't extend CI_Input with MY_Input that has static methods, or at least nothing I tried would work.

Consider this file in application/libraries:


PHP Code:
class Inpoot {


    public static $input_instance NULL;

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

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

            self::$input_instance $CI->input;
        }

        return self::$input_instance;
    }

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

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

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

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



Then in your controller, or perhaps MY_Controller:


PHP Code:
include APPPATH 'libraries/Inpoot.php'

Now you have access to the Inpoot static class that gives you access to CI's Input class methods as static:


PHP Code:
echo Inpoot::get('foo'); 

This may seem silly, but it is what it is. If CodeIgniter had namespaced their Input class, then you could call my Inpoot class simply "Input", but they didn't. This kind of shows the significance of namespacing.
Reply


Messages In This Thread
Why can't we use static objects? - by rakibtg - 07-04-2015, 01:17 PM
RE: Why can't we use static objects? - by kenjis - 07-04-2015, 04:19 PM
RE: Why can't we use static objects? - by skunkbad - 07-04-2015, 09:28 PM
RE: Why can't we use static objects? - by rakibtg - 07-05-2015, 08:32 AM
RE: Why can't we use static objects? - by kenjis - 07-04-2015, 09:51 PM
RE: Why can't we use static objects? - by rakibtg - 07-06-2015, 05:34 AM
RE: Why can't we use static objects? - by kenjis - 07-06-2015, 02:55 PM
RE: Why can't we use static objects? - by rakibtg - 07-06-2015, 11:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB