Array for public variable in php

I have an array, I want its value to be "public $ somevariable". Here is my function of how it looks.

public function __construct(){
        global $database;
        $result = $database->query("SHOW COLUMNS FROM ".self::$tabel_name."");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
            exit;
        }
        if (mysql_num_rows($result) > 0) {
            $attributes_var = array();
            while ($row = $database->fetch_array($result)) {
                $attributes_var[] = $row[0];
            }
        }
        foreach($attributes_var as $key)
        {
            public $$key;
        }
    }

But a syntax error is shown on "public $$ key". I want to use a dynamic generated variable as a public variable and want to use it outside the class.

Any suggestion?

Thank!

+4
source share
1 answer

You can set the default value as shown below null.

foreach($attributes_var as $key)
{
    $this->{$key} = null;
}
+6
source

Source: https://habr.com/ru/post/1528059/


All Articles