-1

I have another idea to improve my code. I want to generate a common call to my functions without neede to repeat all.

This question is related with this one :

Php reading object variable as part of other variable

This is my code:

function link_01() {}
function link_02() {}
..
function link_0N() {}

What I cant to do is the following :

$link_name = 'link_0'                                            ;

for($cont=1 ; $cont <= $this->num_links; $cont++)
{
    $linkName                   = sprintf($link_name , $cont)     ; 
    $fieldLink                 = $this->$linkName               ;       
    $fieldLink($cont)                                              ;    
}

This don't work. What's wrong in my code?

0

2 Answers 2

0

If you use sprinf you need to use a type specifier (http://php.net/manual/ro/function.sprintf.php)

$link_name = 'link_0%d'                                            ;

for($cont=1 ; $cont <= $this->num_links; $cont++)
{
    $linkName                   = sprintf($link_name , $cont)     ; 
    $fieldLink                 = $this->$linkName               ;       
    $fieldLink($cont)                                              ;    
}
Sign up to request clarification or add additional context in comments.

Comments

-1

You can try call_user_func

Here is document:

http://php.net/manual/en/function.call-user-func.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.