3

所以,这是我的情况:

  • 我正在使用 CodeIgniter
  • 我已经设置了一个助手,('DK'文件夹下的'string_helper')
  • 我已经在 dk/string_helper.php 中设置了 dkString 类

    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);)

笔记 :

  • 我已经加载了驻留在自定义助手中的类的静态方法,比如 100 次。所以这没有什么问题。
  • 函数是否声明为无关紧要static。通常它可以正常工作,无论如何。

但是,我不断收到以下错误:

致命错误:在第 XX 行的 /the/path/to/the/caller/file.php 中调用未定义的方法 dkString::strInArray()

有什么想法可能是错的吗?


<?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');)后,我尝试了:

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 )

简而言之:该类已加载,并且它“看到”了所有方法(最后一个方法除外)。天哪……:/

4

1 回答 1

1

尝试制作“公共”功能。

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

    return false;
}

编辑:您的口译员可能找不到课程。然后他找不到静态方法。也许您可以使用class_exists检查该类是否存在并已加载。

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

http://php.net/manual/de/language.oop5.static.php

所以没有人在聊天......但错误信息真的很清楚。您尝试调用静态函数,但该函数不是静态函数,因此您会得到最重要的消息。

否则将它们作为函数调用

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

当您使用像in_array这样的内部函数在数组中查找字符串时,可能会更容易。

于 2012-08-05T10:45:36.237 回答