Passing values between functions in PHP

0

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

How is this done in practice in PHP?

I tried to do something similar but it did not 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')
    
asked by anonymous 02.07.2017 / 04:27

1 answer

2

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

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

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

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

    
02.07.2017 / 16:05