本文介绍了调用静态方法时发生致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这是我的情况:


  • 我正在使用CodeIgniter

  • 我已经设置一个助手,(在'DK'文件夹下的'string_helper')

  • 我已经在dk / string_helper.php

  • I'm using CodeIgniter
  • I've set up a helper, ('string_helper' under 'DK' folder)
  • I've set up the dkString class in dk/string_helper.php

static function strInArray($str,$arr)
{
      foreach ($arr as $item)
      {
            if (self::inString($str,$item))
                  return true;
      }

      return false;
}


  • 在我的控制器中:

    • 我正在加载帮助程序( $ this-> load-> helper('dk / string');

    • 调用方法( dkString :: strInArray($ str,$ arr);

    • In my controller:
      • I'm loading the helper ($this->load->helper('dk/string');)
      • Calling the method (dkString::strInArray($str,$arr);)
      • 注意:


        • 我已经将类的静态方法加载到自定义帮助程序中,如100次。因此,它没有任何问题。

        • 该函数是否声明为 static 无关紧要。

        • I've loaded class's static methods residing in a custom helper, like 100 times. So there's nothing wrong with it.
        • It doesn't matter whether the function is declared as static or not. Normally it works, no matter what.

        但是,我一直收到以下错误:

        However, I keep getting the following error :

        任何想法可能有什么问题吗?

        Any ideas what might be wrong?

        <?php
        
        /*
         * DK4PHP Library
         *
         * Copyright (c) 2010-2012 by Dr.Kameleon
         *
         * This program is free software: you can redistribute it and/or modify
         * it under the terms of the GNU Lesser General Public License as published
         * by the Free Software Foundation, either version 3 of the License, or
         * (at your option) any later version.
         *
         * This program is distributed in the hope that it will be useful,
         * but WITHOUT ANY WARRANTY; without even the implied warranty of
         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
         * GNU General Public License for more details.
         *
         * You should have received a copy of the GNU Lesser General Public License
         * along with this program.  If not, see <http://www.gnu.org/licenses/>.
         *
         */
        
        if (!class_exists('dkString'))
        {
            class dkString
            {
            /**
             * Return number of character in string
             *
             * @param type $str
             * @return type
             */
        
            function characterCount($str)
            {
                return strlen($str);
            }
        
            /**
             * Check if str begins with...
             * @param type $str
             * @param type $sub
             * @return type
             */
        
            function beginsWith($str, $sub)
            {
                return (substr($str,0,strlen($sub)) == $sub);
            }
        
            /**
             * Check if str ends with...
             *
             * @param type $str
             * @param type $sub
             * @return type
             */
        
            function endsWith($str, $sub)
            {
                return (substr($str,strlen($str)-strlen($sub)) == $sub);
            }
        
            /**
             * Check if str contains substring
             *
             * @param type $sub
             * @param type $str
             * @param type $casesensitive
             * @return type
             */
        
            function inString($sub, $str, $casesensitive = false)
            {
                if ($casesensitive)
                return (strstr($str, $sub) == false) ? false : true;
                else
                return (stristr($str, $sub) == false) ? false : true;
            }
        
            /**
             * Count number of occurences of substring in string
             *
             * @param type $sub
             * @param type $str
             * @return type
             */
        
            function inStringCount($sub, $str)
            {
                return substr_count($str, $sub);
            }
        
            /**
             * Convert string to number
             *
             * @param type $str
             * @param type $check
             * @param type $magic
             * @return type
             */
        
            function strtonum($str, $check, $magic)
            {
                $int32Unit = 4294967296;
        
                $length = strlen($str);
                for ($i = 0; $i < $length; $i++)
                {
                $check *= $magic;
                if ($check >= $int32Unit)
                {
                    $check = ($check - $int32Unit * (int) ($check / $int32Unit));
        
                    $check = ($check < -2147483648) ? ($check + $int32Unit) : $check;
                }
                $check += ord($str{$i});
                }
                return $check;
            }
        
            /**
             * Get index of str in array (check if is contained)
             *
             * @param type $str
             * @param type $arr
             */
        
            function indexInArray($str,$arr)
            {
                foreach ($arr as $index=>$item)
                {
                if (stristr($item,$str))
                {
                    return ($index+1);
                    break;
                }
                }
                return -1;
            }
        
            /**
             * Check if str is contained in any of array's elements
             *
             * @param type $str
             * @param type $arr
             * @return boolean
             */
        
            function strInArray($str,$arr)
            {
        
                foreach ($arr as $item)
                {
                    if (self::inString($str,$item))
                        return true;
        
                }
        
                return false;
            }
            }
        }
        ?>
        

        更新

        在我的控制器中,加载了辅助程序( $ this-> load-> helper('dk / string'); )之后,我尝试了:

        In my controller, after loading the helper ($this->load->helper('dk/string');), I tried :

        if (class_exists('dkString')) { echo "IT EXISTS<br/>Methods : "; print_r(get_class_methods('dkString')); }
        else echo "IT DOESN'T EXIST";
        

        有趣的事情是输出为:

        IT EXISTS
        Methods : Array (
        [0] => characterCount
        [1] => beginsWith
        [2] => endsWith
        [3] => inString
        [4] => inStringCount
        [5] => strtonum
        [6] => indexInArray )
        

        简而言之:该类已加载,并且看到了所有方法(除了最后一个)。 Geezz ...:/

        In a few words : the class IS loaded, and it "sees" ALL methods (except for the last one). Geezz... :/

        推荐答案

        尝试使公开 功能。

        public static function strInArray($str,$arr) {
            foreach ($arr as $item) {
                if (self::inString($str,$item))
                    return true;
            }
        
            return false;
        }
        

        编辑:您的口译员可以找不到班级。然后他找不到静态方法。也许您可以与 class_exists 一起检查课程,然后上课。

        Its possible that your interpreter can't find the class. Then he can't find the static method. Perhaps you can check with class_exists wheather the class is there and loaded.

        Edit2:
        您必须将您的函数声明为静态函数。否则,您将无法使用静态运算符(::)调用该函数。

        You have to declare you function as static function. Otherwise you cannot call the function with the static operator(::).

        没有人在聊天...但是错误消息非常明确。您尝试调用静态函数,但该函数不是静态函数,因此您得到的信息就很重要。

        So no one is in chat... but the error message is really clear. You try to call a static function but that function is not a static function so you get the message you get on top.

        否则,将它们作为实例上的函数进行调用

        Otherwise call them as a function over an instance

        $dkString = new dkString;
        $res = $dfString->strInArray();
        

        使用内部功能如 来查找数组中的字符串。

        Perhaps its easier when you use internal functions like in_array to find a string in an array.

        这篇关于调用静态方法时发生致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 07-19 05:04