Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
681 views
in Technique[技术] by (71.8m points)

php - How do I correct this Illegal String Offset?

I am receiving this error "Warning: Illegal string offset 'type' in /home/mysite/public_html/wp-content/themes/evento/lib/php/extra.class.php on line 32"

and I realized this section of code in the file is wrong, however I'm not that great in PHP yet and I am wondering if someone can help me re-write this section to eliminate the error. Thanks! (the error starts on line 32 which is the beginning of the if statement below)

Here is the code:

/* new version */
    function get_attachment_struct( $inputs ){
        $attach = array();

    if( $inputs['type'] == 'attach' ){ 
            $name = $inputs['name'];
            $attach = array(
                0 => array(
                    'name' => $name,
                    'type' => 'text',
                    'label' =>  'Attachment URL',
                    'lvisible' => false,
                    'upload' => true,
                ),
                1 => array(
                    'name' => $name .'_id',
                    'type' => 'hidden',
                    'upload' => true
                ),
            );

            if( isset( $inputs[ 'classes' ] ) ){
                $attach[0]['classes'] = $inputs[ 'classes' ];
                $attach[1]['classes'] = $inputs[ 'classes' ] . '_id';
            }
        }
        return $attach;
    }

    /* new version */
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
if ($inputs['type'] == 'attach') {

The code is valid, but it expects the function parameter $inputs to be an array. The "Illegal string offset" warning when using $inputs['type'] means that the function is being passed a string instead of an array. (And then since a string offset is a number, 'type' is not suitable.)

So in theory the problem lies elsewhere, with the caller of the code not providing a correct parameter.

However, this warning message is new to PHP 5.4. Old versions didn't warn if this happened. They would silently convert 'type' to 0, then try to get character 0 (the first character) of the string. So if this code was supposed to work, that's because abusing a string like this didn't cause any complaints on PHP 5.3 and below. (A lot of old PHP code has experienced this problem after upgrading.)

You might want to debug why the function is being given a string by examining the calling code, and find out what value it has by doing a var_dump($inputs); in the function. But if you just want to shut the warning up to make it behave like PHP 5.3, change the line to:

if (is_array($inputs) && $inputs['type'] == 'attach') {

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...