4

I have a array i want its value to be "public $somevariable". Here is my function how it looks like.

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 its showing syntax error on "public $$key". I want to use the dynamic generated variable as public variable and want to use them outside the class.

Any suggestion?

Thank you!

1 Answer 1

6

You can do like below, the default value is null.

foreach($attributes_var as $key)
{
    $this->{$key} = null;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.