Increment value in a function name

Asked

Viewed 39 times

0

Good afternoon, you guys! Next, I have three file fields in a table of my database, are they: File1, File2 and File3. I also have a get and a set for each of them. The point is, I wanted to run a for() and on each loop increment the get.

    <?php if(isset($pedido) && $pedido->getFile1() != null): ?>
     <?php for($i=0;$i<$count;$i++): ?>
        <div class="list-group">
            <div class="list-group-item">
                <a target="_blank" href="<?php echo '/files/'.$pedido->getFile[$i](); ?>">
                    <i class="fa fa-file-o"></i>
                    <strong>
                    Arquivo <?php echo $i; ?>
                    <i class="fa fa-lock"></i>
                    </strong>
                </a>
                <br />
                <p>
                    <?php echo $pedido->getFile[i](); ?>
                </p>
            </div>
        </div>
     <?php endfor; ?> 
 <?php else: ?>

that is, in each increment would getFile1(), getFile2, getFile3. Is there any way you could do that? I know that syntax [$i] there doesn’t work, it was more to exemplify.

  • So great is the difference between those gets? based on the name use would suffice.

  • No, they are all the same, I am new in php sorry if the question is too ridiculous. As well "by the name use would suffice"?

  • But you have several functions of getFile all numbered ? And they’re all the same/similar ? So it means you don’t have things structured correctly and you need to refactor your code. Create a function getFile that receives the number as parameter and apply only the differences there inside

1 answer

0

I know the following ways to do this:

<?php for($i=0;$i<$count;$i++): ?>
    $nomeMetodo = 'getFile' . $i;

    // 1
    $this->{$nomeMetodo}($arg1, $arg2, $arg3);
    // 2
    $this->$nomeMetodo($arg1, $arg2, $arg3);
    // 3
    call_user_func_array(array('namespaceComNomeDaClasse', $nomeMetodo), array($arg1, $arg2, $arg3));

<?php endfor; ?> 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.