0

Consider the following code:

class Project
{
    public $ProjectID;
}    
class Work
{
    public $WorkID;
}    

public function insert($pData, $tableName)
{
//generate insert here
    $pData->{$tableName . 'ID'} = $result->getId();
}

$p = new Project();
$w = new Work();
insert($w, 'Work');
insert($p, 'Project');

echo $p . ' -- ' . $w;

Now how would I go about setting the variable in a dynamic way? I'm building a data layer. The $pData->{$tableName . 'ID'} doesn't seem to work...

Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
  • Which variable would that be? – George Cummins May 20 '13 at 18:38
  • For `$w` it would be `$w.WorkID = 7` and for `$p` it would be `$p.ProjectID = 7`. – Kees C. Bakker May 20 '13 at 18:39
  • I don't think it is clear what you mean by "dynamic" nor where / why you came up with this function setToSeven, nor what is your problem that you need solving with something "dynamic". – user1122069 May 20 '13 at 18:42
  • 1
    I would be more inclined to use [interfaces](http://php.net/manual/en/language.oop5.interfaces.php) and standardize the name of the property... That way I could have `setToSeven()` just operate on $object->id instead of having a varying property name. – dougBTV May 20 '13 at 18:42
  • missing from the question is what is expected from the above code, and the actual result. I suspect that the actual problem here is in the echo statement. Try `print_r($p)` instead. – Jerry May 20 '13 at 18:46
  • Well I'm building a data layer and I made a simpeler question that the real complex question I actually have :P – Kees C. Bakker May 20 '13 at 18:47
  • 1
    OP wants to create properties dynamically at runtime (ala reflection). Possible duplicate of: http://stackoverflow.com/questions/829823/can-you-create-instance-properties-dynamically-in-php – qJake May 20 '13 at 18:47
  • Would this help here? http://www.php.net/manual/en/language.oop5.overloading.php#object.set – gen_Eric May 20 '13 at 18:53
  • Variables do not need to be created at runtime. Only set dynamically. – Kees C. Bakker May 20 '13 at 23:12

4 Answers4

2

This is what you're looking for:

public function set_to_seven($p_data, $name)
{
    $name = $name . 'ID';
    $p_data->$name = 7;
}

The property name can be a variable. Just like functions:

$p = 'print_r';
$p('StackOverflow');

For future reference: if you need this statically, you're looking for variable variables,

public function set_to_seven($p_data, $name)
{
    $name = $name . 'ID';
    $p_data::$$name = 7;
}

Jordan Doyle
  • 2,976
  • 4
  • 22
  • 38
2

So, you want to dynamically call setters?

$y = new stdClass();
$y->prop1 = "something";

$targetProperty = "prop1";
$y->$targetProperty = "something else";
echo $y->prop1;

//Echos "something else"

That what you're looking for?

dudewad
  • 13,215
  • 6
  • 37
  • 46
1

You can set public properties by accessing them just like any other definition in the class.

$p = new Project();
$p->ProjectID = 5;
echo $p->ProjectID; // prints 5

http://php.net/manual/en/language.oop5.visibility.php

qJake
  • 16,821
  • 17
  • 83
  • 135
  • Yeah, I know, but I need to set properties dynamically. So I don't know the type of the object and I would like a generic function that sets the key. I'm building a object-relational mapper :) – Kees C. Bakker May 20 '13 at 18:44
  • Ah, in that case, this question is a duplicate. See my comment on your question. – qJake May 20 '13 at 18:47
0

This worked for me.

class Project {
    public $ProjectID;
}

function setToSeven($pData, $name) {
    $pData->{$name . "ID"} = 7;
}

$p = new Project();
setToSeven($p, 'Project');

echo $p->ProjectID;

You just need to echo the variable or set up a toString function on the class to echo the class. To String works like this

class Project {
    public $ProjectID;
    public function __toString(){
        return (string)$this->ProjectID;
    }
}

function setToSeven($pData, $name) {
    $pData->{$name . "ID"} = 7;
}

$p = new Project();
setToSeven($p, 'Project');

echo $p;
bottens
  • 3,834
  • 1
  • 13
  • 15