0

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.

danday74
  • 52,471
  • 49
  • 232
  • 283
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Qirel Aug 10 '17 at 19:53

2 Answers2

0

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

Categorize::$arr;
/*$arrlength = count($arr);*/
$arrlength = max(array_map('count', $arr));
Clément Baconnier
  • 5,718
  • 5
  • 29
  • 55
0

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612