Home Backend Development PHP Tutorial Laravel 5.5) Loading process instance method

Laravel 5.5) Loading process instance method

Feb 18, 2018 pm 02:04 PM
laravel method

In bootstrap/app.php

/**
 * 对于其中的instance register singleton 方法到时候单独拎出来说明
 * 
 * 1.设置基础路径
 * 2.使用instance 方法 绑定app 和Illuminate\Foundation\Application类的关系
 * 3.使用instance 方法 绑定Container 和Illuminate\Foundation\Application类的关系
 * 4.app变量中注册事件服务EventServiceProvider
 * 5.app变量中注册日志服务LogServiceProvider
 * 6.app变量中注册路由服务RoutingServiceProvider
 * 7.别名的注册(vendor/laravel/framework/src/Illuminate/Foundation/Application.php文件中的 registerCoreContainerAliases 方法) */$app = new Illuminate\Foundation\Application(    realpath(__DIR__.'/../')
);
Copy after login

Instantiate the vendor/laravel/framework/src/Illuminate/Foundation/Application.php class The magic of this class Method

    public function __construct($basePath = null)
    {        /**
         * 如果有传地址  设置基础路径 设置 
         * path                 $this->path()
         * path.base              $this->basePath()
         * path.lang             $this->langPath()
         * path.config             $this->configPath()
         * path.public            $this->publicPath()
         * path.storage            $this->storagePath()
         * path.database        $this->databasePath()
         * path.resources        $this->resourcePath()
         * path.bootstrap         $this->bootstrapPath()         */
        if ($basePath) {            $this->setBasePath($basePath);
        }        /**
         * 注册 app 和container到 instances数组中         */
        $this->registerBaseBindings();        /**
         * 注册EventServiceProvider LogServiceProvider  RoutingServiceProvider         */
        $this->registerBaseServiceProviders();        /**
         * 设置核心类的别名         */
        $this->registerCoreContainerAliases();
    }
Copy after login

View the registered app and container into the instances array

    protected function registerBaseBindings()
    {        static::setInstance($this);        /**
         * 由于初始化中  app没有添加到instances数组中 所以 不会执行build函数  只做了 instances数组中记录了app和$this         */
        $this->instance('app', $this);        /**
         * 由于初始化中  Container没有添加到instances数组中 所以 不会执行build函数 只做了 instances数组中记录了app和$this         */
        $this->instance(Container::class, $this);
    }
Copy after login


View today’s main methods instance

Flowchart

 public function instance($abstract, $instance)
    {        /**
         * 如果aliases 数组总存在  则游离abstractAliases 数组  删除其中的存在的值         */
        $this->removeAbstractAlias($abstract);        /**
         * 判断  在bindings aliases instances其中有一个存现  则返回true         */
        $isBound = $this->bound($abstract);        /**
         * 删除别名数组中对于的建         */
        unset($this->aliases[$abstract]);        // We'll check to determine if this type has been bound before, and if it has
        // we will fire the rebound callbacks registered with the container and it
        // can be updated with consuming classes that have gotten resolved here.
        /**
         * 在instances 数组中添加对于的建         */
        $this->instances[$abstract] = $instance;        /**
         * 如果之前存在实例化 则运行         */
        if ($isBound) {            $this->rebound($abstract);
        }
    }
Copy after login

The first method removeAbstractAlias

    /**
     * Remove an alias from the contextual binding alias cache.
     *
     * @param  string  $searched
     * @return void     */
    protected function removeAbstractAlias($searched)
    {        /**
         * 如果在别名数组中不存在则直接返回         */
        if (! isset($this->aliases[$searched])) {            return;
        }        /**
         * 游离抽象别名 吧存在抽象类别名数组中的存在的删除
         * @example abstractAliases = ['nameabstract'=>['aliase1','aliases2']]         */
        foreach ($this->abstractAliases as $abstract => $aliases) {            foreach ($aliases as $index => $alias) {                if ($alias == $searched) {                    unset($this->abstractAliases[$abstract][$index]);
                }
            }
        }
    }
Copy after login

The second method Method: bound

    /**
     * Determine if the given abstract type has been bound.
     *
     * @param  string  $abstract
     * @return bool     */
    public function bound($abstract)
    {        return isset($this->bindings[$abstract]) ||               isset($this->instances[$abstract]) ||               $this->isAlias($abstract);
    }
Copy after login

The third method: bound

 /**
     * Fire the "rebound" callbacks for the given abstract type.
     *
     * @param  string  $abstract
     * @return void     */
    protected function rebound($abstract)
    {        /**
         * 主要实现的功能为 build 方法 实例化制定的类 并且返回该类 
         */
        $instance = $this->make($abstract);        /**
         * 查看reboundCallbacks 数组中是否存在该别名创建完成之后需要调用的方法数组 
         * 存在返回需要调用的方法数组   并且逐个执行         */
        foreach ($this->getReboundCallbacks($abstract) as $callback) {            call_user_func($callback, $this, $instance);
        }
    }
Copy after login

The above is the detailed content of Laravel 5.5) Loading process instance method. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

Laravel Redis connection sharing: Why does the select method affect other connections? Laravel Redis connection sharing: Why does the select method affect other connections? Apr 01, 2025 am 07:45 AM

The impact of sharing of Redis connections in Laravel framework and select methods When using Laravel framework and Redis, developers may encounter a problem: through configuration...

Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Apr 01, 2025 am 09:09 AM

Custom tenant database connection in Laravel multi-tenant extension package stancl/tenancy When building multi-tenant applications using Laravel multi-tenant extension package stancl/tenancy,...

Laravel Eloquent ORM in Bangla partial model search) Laravel Eloquent ORM in Bangla partial model search) Apr 08, 2025 pm 02:06 PM

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

How to effectively check the validity of Redis connections in Laravel6 project? How to effectively check the validity of Redis connections in Laravel6 project? Apr 01, 2025 pm 02:00 PM

How to check the validity of Redis connections in Laravel6 projects is a common problem, especially when projects rely on Redis for business processing. The following is...

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Laravel database migration encounters duplicate class definition: How to resolve duplicate generation of migration files and class name conflicts? Laravel database migration encounters duplicate class definition: How to resolve duplicate generation of migration files and class name conflicts? Apr 01, 2025 pm 12:21 PM

A problem of duplicate class definition during Laravel database migration occurs. When using the Laravel framework for database migration, developers may encounter "classes have been used...

See all articles