Home > database >  What is the correct syntax for using a PHP class function to supply the value for a variable in the
What is the correct syntax for using a PHP class function to supply the value for a variable in the

Time:04-21

I am working on a college project that must use a CSV file to create the values for a table. It must have a class and it must have specific class variables (one of them I have already created for testing). Only the variables are specified as having to be part of the class but I think it would be better to pack the function inside the class (unless someone has a different opinion).

Basically, I want to use the class function to create the values for the class variables. Here is my script (refer to the comments for explanation):

    <?php
        class productRow {
            function make_table_row($i) {
                $row = 1;
                $tablerows = [];
                if (($input = fopen("input.csv", "r")) !== FALSE) {
                    while (($tabledata = fgetcsv($input, 1000, ",")) !== FALSE) {   //cycles through the rows of data creating arrays
                        if ($row == 1) {
                            $row  ;     
                            continue;       //skips the first row because it's a header row I don't want on my input
                        }
                        $tablerows[] = $tabledata;      //uses the roles to populate a multidimensional array
                        $row  ;
                    }
                    fclose($input);
                    return $tablerows[$i];      //uses the $i argument to return one of the row arrays
                }
            }
            var $itemNumber = this->make_table_row($i)[0];  //Line 118: calls make_table_row function to get the first item from the row returned
        }
        $pr1 = new productRow;
        echo $pr1->make_table_row(1);       //calls the function from within the class
    ?>

I get this error: Fatal error: Constant expression contains invalid operations in C:\xampp\htdocs\Tylersite\index.php on line 118

I know the return works because I tested it with print_r, including adding an array number as I did with that variable value to get a specific array value. So I must not be calling that function instance correctly. I've tried various things including removing the $this keyword as I wasn't sure I needed it, but the truth is I don't really know how to do it and I'm having trouble finding documentation on the correct syntax. Does anyone know what to do?

CodePudding user response:

Example using constructor function

class productRow {
    var $itemNumber = []; // default value doesn't matter as it will change in __construct

    public function __construct($i) {
        $this->itemNumber = $this->make_table_row($i)[0];
    }

    function make_table_row($i) {
        $row = 1;
        $tablerows = [];

        if (($input = fopen("input.csv", "r")) === FALSE) {
            // return early - reduces indentation
            return;
        }

        while (($tabledata = fgetcsv($input, 1000, ",")) !== FALSE) {   //cycles through the rows of data creating arrays
            if ($row == 1) {
                $row  ;
                continue;       //skips the first row because it's a header row I don't want on my input
            }
            $tablerows[] = $tabledata;      //uses the roles to populate a multidimensional array
            $row  ;
        }
        fclose($input);
        return $tablerows[$i];      //uses the $i argument to return one of the row arrays
    }
}

$pr1 = new productRow(1);
var_dump($pr1->make_table_row(1));
  • Related