views:

3715

answers:

9

Is there any way to create all class properties dynamically ? For example I would like to be able to generate all class attributes from the constructor and still be able to access them after the class is instantiated like this: $class->property.

To be more specific, when I'm dealing with classes that have a large number of attributes I would like to be able to select all columns in a database ( which represent the attributes ) and create class properties from them.

EDIT: Sorry, forgot to mention: Without using an array to hold the properties, each property in a separate 'variable'.

+1  A: 

You can:

$variable = 'foo';
$this->$variable = 'bar';

Would set the attribute foo of the object it's called on to bar.

You can also use functions:

$this->{strtolower('FOO')} = 'bar';

This would also set foo (not FOO) to bar.

Koraktor
+1  A: 

It depends exactly what you want. Can you modify the class dynamically? Not really. But can you create object properties dynamically, as in one particular instance of that class? Yes.

class Test
{
    public function __construct($x)
    {
        $this->{$x} = "dynamic";
    }
}

$a = new Test("bar");
print $a->bar;

Outputs:

dynamic

So an object property named "bar" was created dynamically in the constructor.

Chad Birch
+1  A: 

You can use an instance variable to act as a holder for arbitrary values and then use the __get magic method to retrieve them as regular properties:

class My_Class
{
    private $_properties = array();

    public function __construct(Array $hash)
    {
         $this->_properties = $hash;
    }

    public function __get($name)
    {
         if (array_key_exists($name, $this->_properties)) {
             return this->_properties[$name];
         }
         return null;
    }
}
Carlton Gibson
I personally like your solution as you have more control over how the variables are used in terms on scope and what have you, but he just posted this.EDIT: Sorry, forgot to mention: Without using an array to hold the properties, each property in a separate 'variable'.
The Pixel Developer
Thanks for the comment - I think it's the natural way to go about the problem. I'm afraid I don't understand exactly what Brayn means by his edit — and being new I don't have enough reputation points to post a comment to ask. Oh well...
Carlton Gibson
+5  A: 

Yes, you can.

class test
{
    public function __construct()
    {
     $arr = array
     (
      'column1',
      'column2',
      'column3'
     );

     foreach ($arr as $key => $value)
     {
      $this->$value = '';
     } 
    }

    public function __set($key, $value)
    {
     $this->$key = $value;
    }

    public function __get($value)
    {
     return 'This is __get magic '.$value;
    }
}

$test = new test;

// Results from our constructor test.
var_dump($test);

// Using __set
$test->new = 'variable';
var_dump($test);

// Using __get
print $test->hello;

Output

object(test)#1 (3) {
  ["column1"]=>
  string(0) ""
  ["column2"]=>
  string(0) ""
  ["column3"]=>
  string(0) ""
}
object(test)#1 (4) {
  ["column1"]=>
  string(0) ""
  ["column2"]=>
  string(0) ""
  ["column3"]=>
  string(0) ""
  ["new"]=>
  string(8) "variable"
}
This is __get magic hello

This code will set dynamic properties in the constructor which can then be accessed with $this->column. It's also good practice to use the __get and __set magic methods to deal with properties that are not defined within the class. More information them can be found here.

http://www.tuxradar.com/practicalphp/6/14/2

http://www.tuxradar.com/practicalphp/6/14/3

The Pixel Developer
+4  A: 

Sort of. There are magic methods that allow you to hook your own code up to implement class behavior at runtime:

class foo {
  public function __get($name) {
    return('dynamic!');
  }
  public function __set($name, $value) {
    $this->internalData[$name] = $value;
  }
}

That's an example for dynamic getter and setter methods, it allows you to execute behavior whenever an object property is accessed. For example

print(new foo()->someProperty);

would print, in this case, "dynamic!" and you could also assign a value to an arbitrarily named property in which case the __set() method is silently invoked. The __call($name, $params) method does the same for object method calls. Very useful in special cases. But most of the time, you'll get by with:

class foo {
  public function __construct() {
    foreach(getSomeDataArray() as $k => $value)
      $this->{$k} => $value;
  }
}

...because mostly, all you need is to dump the content of an array into correspondingly named class fields once, or at least at very explicit points in the execution path. So, unless you really need dynamic behavior, use that last example to fill your objects with data.

Udo
This is what I need but will these attributes be private or public? Or can I make them private like so: private $this->{$k} => $value; ?
Brayn
They'll be public, to my knowledge it's not possible to make them private or protected at runtime. For "private" visibility, you could declare a private array-type field and then fill that. Same goes for "protected". It's best to keep things as simple as possible, so you could just introduce a private array called $this->ds where fields inside can then be addressed by statements like $this->ds['fieldname']. Or if you want to be really fancy, you'll implement a mini class that essentially wraps the functionality of an array, so you can do something like $this->myDatasetObject->fieldname.
Udo
You can overload any object in PHP5, without __get and __set, but the properties are always public. If you need to protect things, you need to declare them explicitly.
James Socol
A: 

Here is a sample on this approach:

class User
{
    var $id;
    var $role;

    public function __construct($id)
    {
        if ($id)
        {
            $this->id = $id;
            $this->get();
        }
    }

    function isAdmin()
    {
        return $this->role == 'admin' ? TRUE : FALSE ;
    }

    private function get()
    {
        if ($this->id)
        {
            $query =
            "
                SELECT *
                FROM users
                WHERE user_id='$this->id'
            ";

            // do query, check for error
            $result = @mysql_query($query);
            if ($result)
            {
                $row = @mysql_fetch_assoc($result);
                if ($row)
                {
                    foreach ($row as $i => $v)
                    {
                        // rewrite user_[id|hash] field names
                        $i = str_replace("user_","",$i);

                        // set properties
                        $this->$i = $v;
                    }

                    @mysql_free_result($result);

                    return $row;
                }
            }
        }

        return FALSE;
    }
}

In the get function dynamic properties are basically assigned as follows:

foreach ($row as $i => $v)
{
 // rewrite user_[id|hash] field names
 $i = str_replace("user_","",$i);

 // set properties
 $this->$i = $v;
}

Also notice that I have a isAdmin method, and use $this-role property, because I use the "role" property I need to have it declared above.

farinspace
A: 

This is really complicated way to handle this kind of rapid development. I like answers and magic methods but in my opinion it is better to use code generators like CodeSmith.

I have made template that connect to database, read all columns and their data types and generate whole class accordingly.

This way I have error free (no typos) readable code. And if your database model changes run generator again... it works for me.

zidane
A: 

Here is simple function to populate object members without making class members public. It also leaves constructor for your own usage, creating new instance of object without invoking constructor! So, your domain object doesn't depend on database!


/**
 * Create new instance of a specified class and populate it with given data.
 *
 * @param string $className
 * @param array $data  e.g. array(columnName => value, ..)
 * @param array $mappings  Map column name to class field name, e.g. array(columnName => fieldName)
 * @return object  Populated instance of $className
 */
function createEntity($className, array $data, $mappings = array())
{
    $reflClass = new ReflectionClass($className);
    // Creates a new instance of a given class, without invoking the constructor.
    $entity = unserialize(sprintf('O:%d:"%s":0:{}', strlen($className), $className));
    foreach ($data as $column => $value)
    {
        // translate column name to an entity field name
        $field = isset($mappings[$column]) ? $mappings[$column] : $column;
        if ($reflClass->hasProperty($field))
        {
            $reflProp = $reflClass->getProperty($field);
            $reflProp->setAccessible(true);
            $reflProp->setValue($entity, $value);
        }
    }
    return $entity;
}

/******** And here is example ********/

/**
 * Your domain class without any database specific code!
 */
class Employee
{
    // Class members are not accessible for outside world
    protected $id;
    protected $name;
    protected $email;

    // Constructor will not be called by createEntity, it yours!
    public function  __construct($name, $email)
    {
        $this->name = $name;
        $this->emai = $email;
    }

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function getEmail()
    {
        return $this->email;
    }
}


$row = array('employee_id' => '1', 'name' => 'John Galt', 'email' => '[email protected]');
$mappings = array('employee_id' => 'id'); // Employee has id field, so we add translation for it
$john = createEntity('Employee', $row, $mappings);

print $john->getName(); // John Galt
print $john->getEmail(); // [email protected]
//...

P.S. Retrieving data from object is similar, e.g. use $reflProp->setValue($entity, $value); P.P.S. This function is heavily inspired by Doctrine2 ORM which is awesome!

Sergiy
A: 

Extend stdClass.

class MyClass extends stdClass { public function __construct() { $this->prop=1; } }

I hope this is what you need.

Anthony