3

So, here's my situation :

  • 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;
    }
    
  • In my controller:
    • I'm loading the helper ($this->load->helper('dk/string');)
    • Calling the method (dkString::strInArray($str,$arr);)

Note :

  • 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 :

Fatal error: Call to undefined method dkString::strInArray() in /the/path/to/the/caller/file.php on Line XX

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;
    }
    }
}
?>

UPDATE :

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

The "funny" thing is that the output was :

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

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

6
  • @Yazmat Have you spotted one with an answer to this particular issue? Commented Aug 5, 2012 at 10:47
  • no you made tow questions with same content but different titles :| Commented Aug 5, 2012 at 10:48
  • @Yazmat... Hmmm.. Really??? Ooops, it must have been some issue when I submitted the question... :/ Commented Aug 5, 2012 at 10:49
  • @Yazmat Just deleted it; thanks for letting me know! ;-) Commented Aug 5, 2012 at 10:51
  • You said the folder is DK, but you load it with dk/string, may this be case-sensitive? Commented Aug 5, 2012 at 10:54

1 Answer 1

1

Try to make a "public" function.

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

    return false;
}

Edit: 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(::).

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

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.

Sign up to request clarification or add additional context in comments.

16 Comments

Nope, still not working; I've tried any possible combination (static, public, public static but nothing... :/ )
Hmm perhaps your class is not loaded? You could try to include the class before to test.
If you're not declaring the visibility it falls back to public.
@Stony what do you mean? Check if there's any error when CI loads the helper?
Hmm your functions are not static this is not correct but when they are it should work. Otherwise the file with the class is not loaded over the helper. Perhaps we should make a channel in chat and discuss it there?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.