Passing values between functions in PHP

Asked

Viewed 49 times

0

In Laravel it is common to work with functions like url()->to(string) or redirect()->route(string) where this "second function" ->to(string) extends or returns a value to the first function url().

How this is done in practice in PHP?

I tried to do something similar but it didn’t work:

function to($path)
{
    return $path;
}

/**
 * Return the base theme url.
 *
 * @return string
 */
function url(string $path = null)
{   
    if(isset($path))
        $path = "/{$path}";

    return get_template_directory_uri() . $path;
}

Execution:

url()->to('teste')

1 answer

2


What happens is that the aid function url() of Laravel returns an object of type Illuminate/Routing/UrlGenerator. This class, by itself, has the method to, that returns a string.

Do url()->to("/"), for example, it would be the same as doing:

$url = url();
return $url->to("/");

Where $url is an object of the type UrlGenerator, quoted above.

Browser other questions tagged

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