0
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!

  }

}

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;
1
  • What do you mean "it's problematic?" What happens? That should work. Commented May 18, 2012 at 15:45

3 Answers 3

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

You need to use

$this->$column_name = $column_value

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

public function __get($name) {}

(read about magic methods)

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for late response. I looked at source. What exactly do not work?
0

I think I don't understand your question, but I think you want to use the references operator:

class MyClass {
    protected $name;

    public myfunction() {
        $foo = $this->name;
        $this->name = &$foo;
        // now $foo is a reference to $this->name
    }
}

Comments

0

Um... I think you're asking how to not have to explicitly declare properties. In that case do this:

for($i=0; $i<$num_fields; $i++) {
    $column_name = mysql_field_name($result_set, $i);   
    $this->$column_name =  "";
  }

That will add all the column names as properties to your class, by default as public. Then you can access them using $this->md5, $this->credit, etc...

You should probably look at PDO and returning rows and object and applying a cast to it... PDO has fetch methods where you specify a classname and does this sort of thing automagically. I highly recommend NOT creating your own database abstraction library or ORM as there are already very good ones.

1 Comment

tks that was actually very helpful

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.