I am working on the back end php for a JQuery inline field editing feature for a web app.

I am passing the field name (id) and value (val) to the mysql/update method of my class.

Since this update can be for literally any field in the table (and therefore any property of the object), I need to be able to do this:

function update($id, $val) {
    $this->$id = $val;
}

But I get "cannot access empty property" with this approach.

Using $id, which corresponds to one of the existing object properties, how can I assign $val to the correct property?

    Nevermind. It does work exactly as expected. It helps if $id isn't null when the method is invoked!

      You might want to consider defining and using [man]get[/man] and [man]set[/man] methods instead. They would make the code look nicer and decouple how you access/alter the record fields from how they're implemented inside the object.

        Write a Reply...