所以,这是我的情况:
- 我正在使用 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 )
简而言之:该类已加载,并且它“看到”了所有方法(最后一个方法除外)。天哪……:/