NetizenKing
NetizenKing

Reputation: 95

Accessing PHP 7 Object Properties

I have the following array

$arrdata = array("CTypeID","TypeName","CTYPES","CID","COURSES","CTypeID");

this array is subjected to a function: Generic2DataSideBar($arrdata); The definition of the called function is as follows:

function Generic2DataSideBar($data)
{
    //a generic array for data..
    //var_dump($data);
    $sdata= array();
    //this is the function for generating depended data...
    $this->load->model("Commons");
    $dat="SELECT ".$data[0].",".$data[1]." FROM ".$data[2];
    $result=$this->Commons->LoadResultSet($dat);
    //Looping through the resultset
    foreach ($result as $key) {
        //get the number of departments

        $da="SELECT ".$data[3]." FROM ".$data[4]." WHERE ".$data[5]."='".$key->$data[0]."'";
        $dat=count($this->Commons->LoadResultSet($da));

        $r=array($key->$data[1], $dat);
        array_push($sdata, $r);
    }
    header("content-type: application/json");
    $sdata=json_encode($sdata);
    return $sdata;
}

This function is working well in PHP 5.6.3 but when i upgraded to PHP 7, the line $da="SELECT ".$data[3]." FROM ".$data[4]." WHERE ".$data[5]."='".$key->$data[0]."'"; brings a problem in that $key->$data[0] returns '' All values are OK except that part. In PHP 7 can't one have something like $key->"Data" so long "Data" is a valid property name? Thanks

Upvotes: 0

Views: 1142

Answers (1)

axiac
axiac

Reputation: 72376

As you can see in the documentation, the interpretation of $key->$data[0] changed from PHP 5 to PHP 7 (the second row of the table).

In order to get the same results as on PHP 5 you can do one of these:

  1. extract $data[0] in a variable and use that variable instead:

     $field = $data[0];
     "... WHERE ".$data[5]."='".$key->$field."'";
    
  2. use curly braces to group the sub-expressions that need to be evaluated first:

     "... WHERE ".$data[5]."='".$key->{$data[0]}."'";
    

Both ways are compatible with PHP 5 and they do not change the behaviour of the code on PHP 5.

Upvotes: 3

Related Questions