How do i fill an objects attributes dynamically?
<p>I am trying to save an object with a variable amount of "cols". The number of cols is equal to the number of headers. This is how the code looked before: </p>

<pre><code>if(isset($_POST['submit'])){
    $sub = new Sub();
    $sub-&gt;product_id = $_POST['product_id'];

    $sub-&gt;col1 = $_POST['col1'];
    $sub-&gt;col2 = $_POST['col2'];
    $sub-&gt;col3 = $_POST['col3'];
    $sub-&gt;col4 = $_POST['col4'];
    $sub-&gt;col5 = $_POST['col5'];
    $sub-&gt;col6 = $_POST['col6'];
    $sub-&gt;col7 = $_POST['col7'];
    $sub-&gt;col8 = $_POST['col8'];
    $sub-&gt;col9 = $_POST['col9'];
    $sub-&gt;col10 = $_POST['col10'];
    $sub-&gt;col11 = $_POST['col11'];
    $sub-&gt;col12 = $_POST['col12'];
    $sub-&gt;col13 = $_POST['col13'];
    $sub-&gt;col14 = $_POST['col14'];
    $sub-&gt;col15 = $_POST['col15'];
</code></pre>

<p>This is how I want it to look: </p>

<pre><code>if(isset($_POST['submit'])){
    $sub = new Sub();
    $sub-&gt;product_id = $_POST['product_id'];
    $i = 0;
    foreach($headers as $header){
        $i++ ;
        $sub-&gt;col.$i = $_POST['col'.$i];
    }
</code></pre>

<p>How do I pass the variable $i into the object's attributes? $sub->(col.$i) ? $sub->(col{$i}) ? Please help me figure this out =) Thank you</p>
