0

In my PHP class I have

public $a;
public $b;
public $c;
public $d;

and I set there values in the construct.

I am now attempting to write a update function, and i'm trying to check if they are updating, say $a, to be the same as it is.

function update($what, $to) {

     if ($to == $this->what) return false;
     ...
}

$updated = $instance->update($a, "Foobar");
if ($updated) echo "Updated";
else echo "You didn't change the value";

but since I know this line

         if ($to == $this->what) return false;

is invalid, i'm seeking a new way to write it.

Ideas?

Thanks in advance

willium
  • 2,048
  • 5
  • 25
  • 34
  • 1
    You should also check out the magic `__set` function. – Maerlyn Dec 30 '10 at 20:38
  • looks interesting. Instead of public vars, I use an aray, populate it in the construct, and then use the overload functions? seems easy enough, will try it out! – willium Dec 30 '10 at 20:48
  • possible duplicate of [Get PHP class property by string](http://stackoverflow.com/questions/804850/get-php-class-property-by-string) – outis Jul 27 '12 at 09:11

3 Answers3

2

The solution to your dilemma are variable variables. Your update function must assign it like this:

 $this->{$what} = $to;

The if-check would be correspondingly:

 if ($to == $this->{$what}) return false;

And you cannot actually invoke the update() method with a variable $a. You must give it a variable name as string:

 $instance->update("a", "Foobar");
mario
  • 144,265
  • 20
  • 237
  • 291
  • is it possible to use a prepared statement for a field name? I am trying to get $sql = "UPDATE users SET :what = :to WHERE `id` = :user_id"; to work – willium Dec 30 '10 at 21:01
  • @wills: Not sure where you are going. But variable and attribute names are indeed very free-form. You can assign `$this->{':user_id'}` if you want. – mario Dec 30 '10 at 21:05
  • i'm talking about in a SQL select. the :what is not letting me assign it as a field name in a sql table – willium Dec 30 '10 at 21:12
  • @wills: Are you using PDO for that? Because this `:column` syntax won't work with good old mysql_query. Or if you want to use it as raw column name (you shouldn't) then it needs to be enclosed in SQL backticks rather. - But maybe you should open a new question, show more code. I'm pretty unsure where and how you are using it. – mario Dec 30 '10 at 21:21
  • I am using pdo. I added my whole update class to here https://gist.github.com/7e0cd3f27f3b29dccd96 – willium Dec 30 '10 at 21:47
  • @wills: Okay, I see the problem. You cannot actually use `:what` for column names. Prepared statements can only interpolate data, not column names. You'll have to find a workaround. -- I'm for exmaple using `:?` as column [name placeholder syntax](http://stackoverflow.com/questions/3696327/extended-placeholders-for-sql-e-g-where-id-in). implementation in [php7framework](http://sourceforge.net/p/php7framework/wiki/db/). – mario Dec 30 '10 at 22:32
1

You can do something like:

if ($to == $this->$what) return false;

And call it like this:

update("a", "Foobar");

This uses variable variables ( http://php.net/manual/en/language.variables.variable.php ).


You could also pass by reference:

function update(&$what, $to) {
     if ($to == $what) return false;
        ...
}

And call it like you did in your example.

zsalzbank
  • 9,685
  • 1
  • 26
  • 39
  • is it possible to use a prepared statement for a field name? I am trying to get $sql = "UPDATE users SET :what = :to WHERE id = :user_id"; to work – willium Dec 30 '10 at 21:02
0

$this->$what should do the trick, if $what = 'a'

You generally want to avoid doing that though.

Zahymaka
  • 6,523
  • 7
  • 31
  • 37