Home > Backend Development > PHP Tutorial > Laravel中类中的构造函数传参是可以自动new一个传递进去的吗?

Laravel中类中的构造函数传参是可以自动new一个传递进去的吗?

WBOY
Release: 2016-06-06 20:40:57
Original
1651 people have browsed it

这是LaravelAuth\Guard的构造函数:

<code>    /**
     * Create a new authentication guard.
     *
     * @param  \Illuminate\Auth\UserProviderInterface  $provider
     * @param  \Illuminate\Session\Store  $session
     * @param  \Symfony\Component\HttpFoundation\Request  $request
     * @return void
     */
    public function __construct(UserProviderInterface $provider,
                                SessionStore $session,
                                Request $request = null)
    {
        $this->session = $session;
        $this->request = $request;
        $this->provider = $provider;
    }
</code>
Copy after login
Copy after login

其中传入了参数SessionStore $session
但是session的构造函数是这样的:

<code>public function __construct($name, SessionHandlerInterface $handler, $id = null)
    {
        $this->setId($id);
        $this->name = $name;
        $this->handler = $handler;
        $this->metaBag = new MetadataBag;
    }
</code>
Copy after login
Copy after login

这里是有参数的,为什么Guard的构造函数可以自动生成session?
php原生提供的还是Laravel提供的?

回复内容:

这是LaravelAuth\Guard的构造函数:

<code>    /**
     * Create a new authentication guard.
     *
     * @param  \Illuminate\Auth\UserProviderInterface  $provider
     * @param  \Illuminate\Session\Store  $session
     * @param  \Symfony\Component\HttpFoundation\Request  $request
     * @return void
     */
    public function __construct(UserProviderInterface $provider,
                                SessionStore $session,
                                Request $request = null)
    {
        $this->session = $session;
        $this->request = $request;
        $this->provider = $provider;
    }
</code>
Copy after login
Copy after login

其中传入了参数SessionStore $session
但是session的构造函数是这样的:

<code>public function __construct($name, SessionHandlerInterface $handler, $id = null)
    {
        $this->setId($id);
        $this->name = $name;
        $this->handler = $handler;
        $this->metaBag = new MetadataBag;
    }
</code>
Copy after login
Copy after login

这里是有参数的,为什么Guard的构造函数可以自动生成session?
php原生提供的还是Laravel提供的?

https://github.com/laravel/framework/blob/4.2/src/Illuminate/Auth/AuthManager.php#L51

<code>/**
 * Create an instance of the Eloquent driver.
 *
 * @return \Illuminate\Auth\Guard
 */
public function createEloquentDriver()
{
    $provider = $this->createEloquentProvider();

    return new Guard($provider, $this->app['session.store']);
}
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template