本文介绍了php oop有自动设置此变量的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class User extends DatabaseObject {

    protected static $table_name='users';
    protected static $db_fields = array();

public  function __construct() {

    // Get columns from table
    global $database;
    $result_set = $database->query("SELECT * FROM ".self::$table_name." LIMIT 1");
    $num_fields = mysql_num_fields($result_set);
    for($i=0; $i<$num_fields; $i++) {
        $column_name = mysql_field_name($result_set, $i);
        // Set column names as variables
        self::$db_fields[] = $column_name; // THIS WORKS
    $this->{$column_name}; // THIS IS PROBLEMATIC!

  }

}

例如此$name = pulic $wherever;所以我可以打电话给wherever->wherever;

for example this $name = pulic $wherever;so I can call, for example, wherever->wherever;

这样我就不必在每次添加变量时都键入此名称

so that I don't have to type this name every time I add the variables

public $md5;
public $credit;
public $pontaria_time;
public $Credits;
public $airoplayne;
public $city_id;
public $prisao_time;
public $crime2;
public $banck_time;

推荐答案

 $this->{$column_name}; // THIS IS PROBLEMATIC!

您需要使用

$this->$column_name = $column_value

或者您可以在$ db_fields字段中写入所有列并添加

OR you can write all columns in $db_fields field and add

public function __get($name) {}

(了解有关魔术方法)

这篇关于php oop有自动设置此变量的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 17:59