0

I couldn't find about my issue on google and SO. Hope, i can explain you.

You'll understand when looked following function:

function get_page($identity)
{
    if($identity is id)
       $page = $this->get_page_from_model_by_id($identity);
    elseif($identity is alias)
       $page = $this->get_page_from_model_by_alias($identity);
}

My used function:

get_page(5); // with id
or
get_page('about-us'); // with alias
or
get_page(5, 'about-us'); // with both

I want to send parameter to function id or alias. It should be only one identifier.

I dont want like function get_page($id, $alias)

How can i get and know parameter type with only one variable. Is there any function or it possible?

5
  • Are id and alias classes, and the param passed is object of that class? Commented Sep 19, 2013 at 13:56
  • 2
    func_get_args() return the given arguments as an array Commented Sep 19, 2013 at 13:56
  • 2
    if($identity instanceof id) ? Commented Sep 19, 2013 at 13:57
  • is_int($identity) shouldn't do it just fine? Commented Sep 19, 2013 at 13:58
  • can't you just call get_page_from_model_by_id and get_page_from_model_by_alias directly? Commented Sep 19, 2013 at 14:00

5 Answers 5

1
if(is_numeric($identity)) {
    $page = $this->get_page_from_model_by_id($identity);
}
elseif(is_string($identity)) {
    $page = $this->get_page_from_model_by_alias($identity);
}
elseif(func_num_args() === 2) {
    $id = func_get_arg(0);
    $alias = func_get_arg(1);
    //do stuff
}
Sign up to request clarification or add additional context in comments.

2 Comments

I know func_num_args. I was searching function that give me the variable name/type directly. But it's ok. Btw i changed is_int to is_numeric because of i'm getting identity from uri and it's always string.
@Bora you should test in the opposite order then, because the number will probably evaluate to true if tested as a string. I edited.
1

Use is_string() to find whether input is integer or character.

Comments

0

You should make use of func_get_args()

<?php
function foo()
{
    $numargs = func_num_args();
    echo "Number of arguments: $numargs<br />\n";
    if ($numargs >= 2) {
        echo "Second argument is: " . func_get_arg(1) . "<br />\n";
    }
    $arg_list = func_get_args();
    for ($i = 0; $i < $numargs; $i++) {
        echo "Argument $i is: " . $arg_list[$i] . "<br />\n";
    }
}

foo(1, 2, 3);
?>

Source

Comments

0

Here's a complete solution:

function get_page()
{
    $alias = false;
    $id = false;
    foreach(func_get_args() as $arg)
        if(is_string($arg))
             $alias = $arg;
        else if(is_int($arg))
             $id = $arg;

}

Comments

0

Assuming id is always a number, you could test for it with php's is_numeric()

function get_page($identity)
{
    if(is_numeric($identity) {
       $page = $this->get_page_from_model_by_id($identity);

    } else { 
       $page = $this->get_page_from_model_by_alias($identity);
    }
}

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.