Rather than write a huge list of properties used by a class I want to know if I can dynamically generate them when needed. I have looked and __set and __get but I am not sure how (or if) I use them with what I am trying to do.

The function I have at the moment is this (obviously doesn't work):

public function getShowHelpingDetails($id) {                
$this->show_id = $id;        
$result = $this->selectRecord($this->table, $this->show_id, '*', '', 'show_');       
foreach ($result as $key => $value) {            
  $this->{$key} = $value;       
}            
}

And then trying to access the properties like this:

$h = new ShowHelping;
     $helping = $h->getShowHelpingDetails($show_id);
echo $helping->time;  

Can anyone help me with this and if not possible advise of a better way to set my class properties without huge lists of them?

Yes, you can dynamically set the value of instance variables within an object.

As best I can determine from the code you have posted, if there is a key called time and it has a value, then it should work and be accessible.

Can you var_dump $result on line 3 and post the contents?

The magic methods __set and __get are called when you try to access an instance variable on an object where the variable doesn't exist.

E.g.

$show_helping = new ShowHelping;

echo $show_helping->time;   // This would pass to __get as time is not set

$show_helping->time = '13:16:00';
echo $show_helping->time;   // This would output '13:16:00'

I have dumped the array already and run lots of checks and all is fine but this is what I get:

array(10) { ["id"]=> string(2) "44" ["show_id"]=> string(2) "12" ["job"]=> string(1) "1" ["time"]=> string(1) "1" ["judge"]=> string(1) "0" ["ring_party"]=> string(1) "0" ["hours"]=> string(1) "1" ["club"]=> string(1) "1" ["job_share"]=> string(1) "0" ["help_text"]=> string(403) "....... (replaced text) " }

Would the array value types be causing a problem???

No, the array value types wouldn't be a problem.

Here's a simplied example, which does work:

class Dynamic
{
    public function load()
    {
        $attributes = array(
            'id' => 44,
            'show_id' => 12,
            'job' => 1,
            'time' => 1,
            'judge' => 0,
            'ring_party' => 0,
            'hours' => 1,
            'club' => 1,
            'job_share' => 0,
            'help_text' => 'some text',
        );

        foreach($attributes as $attribute => $value)
            $this->{$attribute} = $value;

        return $this;
    }
}

$dynamic = new Dynamic;
$dynamic->load();
echo '<pre>'; var_dump($dynamic); echo '</pre>'; die;

The output when var dumping the data is:

object(Dynamic)#1 (10) {
  ["id"]=>
  int(44)
  ["show_id"]=>
  int(12)
  ["job"]=>
  int(1)
  ["time"]=>
  int(1)
  ["judge"]=>
  int(0)
  ["ring_party"]=>
  int(0)
  ["hours"]=>
  int(1)
  ["club"]=>
  int(1)
  ["job_share"]=>
  int(0)
  ["help_text"]=>
  string(9) "some text"
}

If it's still not working for you, what's the output from your class if you var_dump it immediately after loading the data and setting the attributes?

OMG - that was so simple to sort after using your script (which worked) and looking at mine again as basically I was missing the return which I didn't think I needed as I was just accessing the class properties from the object as I normalluy would.

Thank you so much for the help.

Ah, you only need the return if you want to overwrite the value of $helping.

It doesn't work the way I want it without the return so I will stick with it :-)

This was your original code:

$h = new ShowHelping;
$helping = $h->getShowHelpingDetails($show_id);
echo $helping->time;  

The problem here is that you made a new class instance, called $h, but you then tried to access the attribute time on a object called $helping.

To fix it:

$h = new ShowHelping;
$h->getShowHelpingDetails($show_id);
echo $h->time;

Then you don't need the return if you don't want it.

It is however useful for allowing you to chain methods.

I have to do it this way as I am using the instance of the object more than once so I have to use a variable for each instance. Thank you anyway, it has clarified for the future.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.