Welcome Guest, Not a member yet? Register   Sign In
Override core/input CI3
#1
Exclamation 

Hello folks,

All was well until I tried to upgrade to CI 3. This is the error I am getting, and don't really know why? Angry

Code:
A PHP Error was encountered

  Severity: 4096

  Message: Argument 1 passed to get_config() must be of the type array, string  
  given, called in  
  XAMPP/xamppfiles/htdocs/test/application/core/MY_Input.php on  
  line 6 and defined

  Filename: core/Common.php

  Line Number: 238

This is all I have on MY_Input.php

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

       Class MY_Input extends CI_Input
    {

       function _clean_input_keys($str)
     {
    $config = &get_config('config');  **//  LINE 6**

    if ( ! preg_match("/^[".$config['permitted_uri_chars']."]+$/i",

    rawurlencode($str)))

     {
        exit('Disallowed Key Characters.');
     }

    // Clean UTF-8 if supported
    if (UTF8_ENABLED === TRUE)
    {
        $str = $this->uni->clean_string($str);
    }

    return $str;
    }

    }

This is a system file:
Filename: core/Common.php
Line Number: 238

Code:
function &get_config(Array $replace = array())  **// LINE 238**
    {
    static $config;

    if (empty($config))
    {
        $file_path = APPPATH.'config/config.php';
        $found = FALSE;
        if (file_exists($file_path))
        {
            $found = TRUE;
            require($file_path);
        }

        // Is the config file in the environment folder?
        if (file_exists($file_path =
         APPPATH.'config/'.ENVIRONMENT.'/config.php'))
        {
            require($file_path);
        }
        elseif ( ! $found)
        {
            set_status_header(503);
            echo 'The configuration file does not exist.';
            exit(3); // EXIT_CONFIG
        }

        // Does the $config array exist in the file?
        if ( ! isset($config) OR ! is_array($config))
        {
            set_status_header(503);
            echo 'Your config file does not appear to be formatted  
             correctly.';
            exit(3); // EXIT_CONFIG
        }
    }

    foreach ($replace as $key => $val)
    {
        $config[$key] = $val;
    }

    return $config;
    }

Why is it telling me it's not an array since it worked perfectly with previous versions?

Thank you :
Reply
#2

I have this file in application/core/MY_Input.php

On previous versions the function had only one parameter. After upgrading to CI 3, it started throwing an error

Severity: Runtime Notice
Message: Declaration of MY_Input::_clean_input_keys() should be compatible with CI_Input::_clean_input_keys($str, $fatal = true)
Filename: core/MY_Input.php


So I added a param $fatal = true  and the error was gone.

Now I have two different error:

Severity: 4096

Message: Argument 1 passed to get_config() must be of the type array, string given, called in application/core/MY_Input.php on line 7 and defined.  // Line 7 is  $config = &get_config('config');

Filename: core/Common.php

Line Number: 238



Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: core/Common.php

Line Number: 274


PHP Code:
     ]<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
Class 
MY_Input extends CI_Input
{
    function 
_clean_input_keys($str$fatal true)
    {
 
             
        $config 
= &get_config('config');
 
              
        
if ( ! preg_match("/^[".$config['permitted_uri_chars']."]+$/i"rawurlencode($str)))
        {
            exit(
'Disallowed Key Characters.');
        }
    
        
// Clean UTF-8 if supported
        
if (UTF8_ENABLED === TRUE)
        {
            
$str $this->uni->clean_string($str);
        }
 
              
    
        
return $str;
    }
    


I just can't figure out where the issue is??  It worked fine on CI 2.2.1 why not on CI 3?

Thanks
Reply
#3

It's because you're using the function improperly. It accepts an array of key => value pairs to replace in the returned config array, not a string.
Reply
#4

because v2.2.1's method signature looks like this:


PHP Code:
function &get_config($replace = array()) 

The word 'Array' before $replace in the v3 code enforces the type of the argument, so you get an error immediately for passing the wrong type of data to the method. In v2, the type of the argument wasn't checked, so, if you were lucky, you wouldn't get an error. Otherwise, you would get an error on line 260:

PHP Code:
foreach ($replace as $key => $val


The closest thing to a type check in the v2 code for this method was the following check before the foreach() mentioned above:

PHP Code:
if (count($replace) > 0

It really is pretty much luck that an error wasn't encountered, since the documentation of the count() function includes this in the documentation of the return value:

Quote:If the parameter is not an array or not an object with implemented Countable interface, 1 will be returned.
Reply
#5

(02-16-2015, 01:54 AM)Narf Wrote: It's because you're using the function improperly. It accepts an array of key => value pairs to replace in the returned config array, not a string.

How could I match the param for that function?

CI 2.2.1
Code:
function &get_config($replace = array())
    {

CI 3.0.0

PHP Code:
function &get_config(Array $replace = array())
    { 
Reply
#6

Change this:
PHP Code:
$config = &get_config('config'); 


to this:
PHP Code:
$config = &get_config(); 

'config' was not a valid value for that argument, so it should behave the same way it did in v2.x if you don't pass anything.
Reply
#7

(02-16-2015, 11:17 AM)mwhitney Wrote: Change this:

PHP Code:
$config = &get_config('config'); 


to this:

PHP Code:
$config = &get_config(); 

'config' was not a valid value for that argument, so it should behave the same way it did in v2.x if you don't pass anything.

Thank you so much, problem solved Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB