简体   繁体   中英

Fatal error: Uncaught Error: Class 'Categorize' not found in /opt/lampp/htdocs/IRIS/controllers/get_category.php:7

1. In the class below called Get_category, I am trying to access an array variable called $arr from another class called Categorize. But everytime I run my Get_category class, an error shows up(the one in the title) saying that it doesn't recognize the Categorize class.

class Get_category extends Controller{
     function run($xml){
         global $FILE_ROOT, $STORAGE, $REQ_ID, $CMD_EXTRA, $LIB, $BIN;

         $file = $xml->filename;
         Categorize::$arr;
         $arrlength = count($arr);
         $response = "<parameters>\n<requestID>" . $REQ_ID ."</requestID>\n<requestType>get_category</requestType>";

         for($i = 0; $i < $arrlength; $i++){
         $lengthcolumn = count($arr[$i]);
            for($j = 0; $j < $lengthcolumn; $j++){
                if($arr[$i][$j] == $file){
                    echo $arr[$i][$j];
                    $response .= "<resource><id>" . $arr[$i][$j] . "</id>";
                    $response .= "</resource>";
                }

            }

         }

        $response .= "</parameters>";

        return $response; 


    }    
}

This is the other class Categorize that I am trying to access the variable from.

    class Categorize extends Controller{
       function run($xml){
              global $FILE_ROOT, $STORAGE, $REQ_ID, $CMD_EXTRA, $LIB, $BIN;


              $numCategories = intval($xml->numCategories);
              static $arr = array();
              /*self::$arr = array();*/

              /*if(!pe($xml, "resourceList")) die(err("No resources found"));*/

              for($i=0;$i < $numCategories; $i++){
                  $name = intval($xml->nameCat);
                         if($i=0){
                            $arr[0][0] = $name;
                         }else{
                            $arr[$i][0] = $name;
                         }

              }
              $j = 0;
              while($j < $numCategories){
                  $numDoc = intval($xml->numDoc);
                       $k = 0;
                       foreach($xml->resourceList->resource as $res){
                                  $arr[$j][$k] = $res;
                                  $k++;
                       }
                   $j++;         
              }
             $output = "Done!";
             $response = "<parameters><requestType>categorize</requestType><requestID>". $REQ_ID . "</requestID><resourceList>". $output . "</resourceList></parameters>";

             return $response;
       }
}

For some reason I keep getting the error listed in the title and I don't know why? I have been trying to make my $arr variable static and I have been using the proper notation to get my static variable. It says that it doesn't recognize Categorize in get_Category, so I don't know how to fix it so that it realizes that I am trying to access a class outside of the current one. Could there be a problem with my Categorize class itself?

include the file where the class Categorize is imported at to top of the file like this:

include_once 'path/to/Categorizeclass'

for example at the top of controllers/get_category.php add this :

include_once __DIR__.'/categorize.php';

PHP needs help knowing where it can find the classes you are trying to load, unless they are in the same file, or an included or required file. The function spl_autoload_register() is used to provide a function that PHP can use to figure this out.

For example if you have your classes neatly placed in individual files, Categorize.class.php and Get_Category.class.php , and they are placed in a folder /classes , you would use something like this:

spl_autoload_register(function($class){
    require_once(sprintf("/classes/%s.class.php", $class)); 
}

This will give PHP an anonymous function that PHP can give the unknown class' name, so you can call the require or include function yourself, since you know about your folder structures.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM