PHP学习 之 autoload

本文的示例代码参考autoload

目录

  • 环境

  • 开始

  • 没有autoload

  • PHP-autoload

  • SPL-autoload

  • Composer-autoload

    • composer.json

    • vendor/autoload.php

    • vendor/composer/autoload_real.php

    • vendor/composer/autoload_psr4.php

    • vendor/composer/ClassLoader.php

  • 参考

环境

phpbrew

sudo apt install -y \
  php7.0 \
  php7.0-curl \
  php7.0-json \
  php7.0-cgi \
  php7.0-fpm \
  autoconf \
  automake \
  libxml2-dev \
  libcurl4-openssl-dev \
  libssl-dev \
  openssl \
  gettext \
  libicu-dev \
  libmcrypt-dev \
  libmcrypt4 \
  libbz2-dev \
  libreadline-dev \
  build-essential \
  libmhash-dev \
  libmhash2 \
  libxslt1-dev

相关依赖参考 Requirement - Ubuntu 16.04

curl -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew

chmod +x phpbrew && sudo mv phpbrew /usr/local/bin/phpbrew

phpbrew init

cat ~/.phpbrew/bashrc >> ~/.bashrc && . ~/.bashrc
phpbrew known

phpbrew install 7.1.0

phpbrew use 7.1.0 && php -v

开始

vim startup.php
name = $name;
        $this->age = $age;
    }
}

$person = new Person("David", 6);
var_dump($person);

PHP语法参考 X分钟速成Y - 其中 Y=PHP

php startup.php
object(Person)#1 (2) {
  ["name"]=>
  string(5) "David"
  ["age"]=>
  int(6)
}

没有autoload

vim Person.php
name = $name;
        $this->age = $age;
    }
}
vim no_autoload.php
php no_autoload.php
object(Person)#1 (2) {
  ["name"]=>
  string(5) "David"
  ["age"]=>
  int(6)
}

include与require的区别参考 PHP中include和require的区别详解 / php中return,require,include加括号和不加括号的区别

PHP autoload

vim php_autoload.php
php php_autoload.php
__autoload classname: Person
object(Person)#1 (2) {
  ["name"]=>
  string(5) "David"
  ["age"]=>
  int(6)
}

SPL autoload

The Standard PHP Library(SPL) is a collection of interfaces and classes that are meant to solve common problems

vim  spl_autoload.php
php spl_autoload.php
autoload in ./ classname: Person
object(Person)#1 (2) {
  ["name"]=>
  string(5) "David"
  ["age"]=>
  int(6)
}
vim  spl_autoload.php
php spl_autoload.php
autoload in ../ classname: Person
class file../Person.phpnot found!PHP Fatal error:  Uncaught Error: Class 'Person' not found in /Users/kevin/Workspace/php-tutorial/autoload/spl_autoload.php:28

Composer autoload

Composer详细介绍参考 Composer 中文文档

composer.json

cat composer.json
# 其他略过
"autoload": {
    "psr-4": {
        "Base\\": "module/Base/"
    }
},
# 其他略过

Composer autoload参考 Composer 中文文档 - 基本用法

vendor/autoload.php

cat vendor/autoload.php

vendor/composer/autoload_real.php

cat vendor/composer/autoload_real.php
 $path) {
            $loader->setPsr4($namespace, $path);
        }

        $loader->register(true);

        return $loader;
    }
}

为叙述问题方便 这里只保留核心代码 以下也相同处理

vendor/composer/autoload_psr4.php

cat vendor/composer/autoload_psr4.php
 array($baseDir . '/module/Base'),
);

vendor/composer/ClassLoader.php

cat vendor/composer/ClassLoader.php
fallbackDirsPsr4 = (array) $paths;
        } else {
            $length = strlen($prefix);
            if ('\\' !== $prefix[$length - 1]) {
                throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
            }
            $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
            $this->prefixDirsPsr4[$prefix] = (array) $paths;
        }
    }

    public function register($prepend = false)
    {
        spl_autoload_register(array($this, 'loadClass'), true, $prepend);
    }

    public function loadClass($class)
    {
        if ($file = $this->findFile($class)) {
            includeFile($file);

            return true;
        }
    }
}

参考

  • 类的自动加载

  • PHP autoload 机制详解

  • Composer 中文文档 - 基本用法

  • Autoloading Classes In PHP

  • PHP AutoLoad Best Practices

你可能感兴趣的:(PHP学习 之 autoload)