【问题标题】:Fatal Error when calling static method调用静态方法时出现致命错误
【发布时间】:2012-08-02 16:27:10
【问题描述】:

所以,这是我的情况:

  • 我正在使用 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 无关紧要。无论如何,它通常都能正常工作。

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

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

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


<?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-&gt;load-&gt;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 )

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

【问题讨论】:

  • @Yazmat 你有没有发现一个对这个特定问题有答案的人?
  • 不,您提出了两个内容相同但标题不同的问题:|
  • @Yazmat...嗯..真的吗???糟糕,我提交问题时一定是有问题...:/
  • @Yazmat 刚刚删除;谢谢你让我知道! ;-)
  • 没问题 :) 不客气

标签: php class codeigniter static-methods fatal-error


【解决方案1】:

尝试制作一个“public”函数。

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

    return false;
}

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

编辑2: 您必须将您的功能声明为静态功能。否则你不能用静态操作符(::)调用函数。

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

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

否则将它们作为实例调用的函数

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

使用 in_array 等内部函数在数组中查找字符串可能更容易。

【讨论】:

  • 不,还是不行;我尝试了任何可能的组合(staticpublicpublic static,但没有...:/)
  • 嗯,也许你的课程没有加载?您可以尝试在测试之前包含该类。
  • 如果你不声明可见性,它会退回到public
  • @Stony 你是什么意思?检查CI加载helper时是否有错误?
  • 嗯,你的函数不是静态的,这是不正确的,但是当它们存在时它应该可以工作。否则,带有类的文件不会通过帮助程序加载。也许我们应该在聊天中建立一个频道并在那里讨论?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
  • 2016-05-02
  • 2011-05-15
  • 2014-07-03
  • 1970-01-01
相关资源
最近更新 更多