所以,这是我的情况:
我在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);
)注意:
static
并不重要。通常它无论如何都有效。但是,我一直收到以下错误:
致命错误:调用未定义的方法dkString :: strInArray() 第XX行的/the/path/to/the/caller/file.php
任何想法可能出错?
<?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 )
用几句话说:类IS被加载,它“看到”所有方法(除了最后一个)。 Geezz ......:/
答案 0 :(得分: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 等内部函数在数组中查找字符串时,可能会更容易。