简体   繁体   中英

Notice: Undefined variable: arr in /opt/lampp/htdocs/IRIS/controllers/get_category.php on line 9

I am trying to get access to a static variable from another class, but I keep getting an error (the one in the title) that says my variable is not being recognized.

Below is my Categorize class where the static variable, which is an array named $arr, is located:

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

              $numCategories = intval($xml->numCategories);
              self::$arr;
              /*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;
       }
 }

Here is a class called Get_category where I am trying to access the static variable $arr from my the Categorize class:

     class Get_category extends Controller{
            function run($xml){
            global $FILE_ROOT, $STORAGE, $REQ_ID, $CMD_EXTRA, $LIB, 
            $BIN;
            include_once __DIR__.'/categorize.php';
            $file = $xml->filename;
            Categorize::$arr;
            /*$arrlength = count($arr);*/
            $arrlength = max(array_map('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; 

         }

}

I don't understand why my $arr variable is being unrecognized.

Looks like you forgot to assign $arr = Categorize::$arr;

Categorize::$arr;
/*$arrlength = count($arr);*/
$arrlength = max(array_map('count', $arr));

In PHP you can't access class properties using regular variable syntax. Static properties are accessed using self::$property , object properties are accessed using $this->property .

So where you have $arr it should be self::$arr . This change is needed in both functions that you posted. It doesn't cause an error in the first function because it's assigning to the variable rather than reading it, so it creates the variable as well. But I assume that the intent was to fill in the public static $arr property, which is not happening because it's being accessed incorrectly.

class Categorize extends Controller{

    public static $arr = array();

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

        $numCategories = intval($xml->numCategories);
        self::$arr;
        /*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){
                self::$arr[0][0] = $name;
            }else{
                self::$arr[$i][0] = $name;
            }

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

        return $response;
    }
}

class Get_category extends Controller{
    function run($xml){
        global $FILE_ROOT, $STORAGE, $REQ_ID, $CMD_EXTRA, $LIB, 
            $BIN;
        include_once __DIR__.'/categorize.php';
        $file = $xml->filename;
        /*self:$arrlength = count(self:$arr);*/
        $arrlength = max(array_map('count', self::$arr));
        $response = "<parameters>\n<requestID>" . $REQ_ID ."</requestID>\n<requestType>get_category</requestType>";

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

            }

        }

        $response .= "</parameters>";

        return $response; 

    }

}

This is a significant difference between PHP and some other OOP languages like C++ and Java. See Could not retrieve a property of class in PHP for my explanation of the rationale of this.

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