Laravel框架用到的函数总结

431 阅读1分钟

spl_autoload_register

在 laravel 中的代码

spl_autoload_register(array('ComposerAutoloaderInitdfa4653e177b9db7c9afe9170d01dccf', 'loadClassLoader'), true, true);

作用:spl_autoload_register — 注册给定的函数作为 __autoload 的实现

spl_autoload_unregister

作用:注销已注册的 __autoload() 函数

call_user_func

在laravel中的代码

call_user_func(\Composer\Autoload\ComposerStaticInitdfa4653e177b9db7c9afe9170d01dccf::getInitializer($loader));


public static function getInitializer(ClassLoader $loader)
{
    return \Closure::bind(function () use ($loader) {
        $loader->prefixLengthsPsr4 = ComposerStaticInitdfa4653e177b9db7c9afe9170d01dccf::$prefixLengthsPsr4;
        $loader->prefixDirsPsr4 = ComposerStaticInitdfa4653e177b9db7c9afe9170d01dccf::$prefixDirsPsr4;
        $loader->prefixesPsr0 = ComposerStaticInitdfa4653e177b9db7c9afe9170d01dccf::$prefixesPsr0;
        $loader->classMap = ComposerStaticInitdfa4653e177b9db7c9afe9170d01dccf::$classMap;

    }, null, ClassLoader::class);
}

作用:把第一个参数作为回调函数调用

Closure::bind

在laravel 中的代码

public static function getInitializer(ClassLoader $loader)
{
    return \Closure::bind(function () use ($loader) {
        $loader->prefixLengthsPsr4 = ComposerStaticInitdfa4653e177b9db7c9afe9170d01dccf::$prefixLengthsPsr4;
        $loader->prefixDirsPsr4 = ComposerStaticInitdfa4653e177b9db7c9afe9170d01dccf::$prefixDirsPsr4;
        $loader->prefixesPsr0 = ComposerStaticInitdfa4653e177b9db7c9afe9170d01dccf::$prefixesPsr0;
        $loader->classMap = ComposerStaticInitdfa4653e177b9db7c9afe9170d01dccf::$classMap;

    }, null, ClassLoader::class);
}

作用:复制一个闭包,绑定指定的$this对象和类作用域。

strstr

$logicalPathPsr4 = strtr($class, '\', DIRECTORY_SEPARATOR) . $ext;
这里返回命名空间的首个英文

作用:查找字符串的首次出现

substr

$logicalPathPsr4 = strtr($class, '\', DIRECTORY_SEPARATOR) . $ext;

$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
    $subPath = $class;
    while (false !== $lastPos = strrpos($subPath, '\')) {
        $subPath = substr($subPath, 0, $lastPos);
        $search = $subPath . '\';
        if (isset($this->prefixDirsPsr4[$search])) {
            $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
            foreach ($this->prefixDirsPsr4[$search] as $dir) {
                if (file_exists($file = $dir . $pathEnd)) {
                    return $file;
                }
            }
        }
    }
}

这段代码作用其实寻找 ps4中是否有满足类名的首字母,如果有继续往下找

作用:返回字符串的子串

strpos

$logicalPathPsr4 = strtr($class, '\', DIRECTORY_SEPARATOR) . $ext;

$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
    $subPath = $class;
    while (false !== $lastPos = strrpos($subPath, '\')) {
        $subPath = substr($subPath, 0, $lastPos);
        $search = $subPath . '\';
        if (isset($this->prefixDirsPsr4[$search])) {
            $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
            foreach ($this->prefixDirsPsr4[$search] as $dir) {
                if (file_exists($file = $dir . $pathEnd)) {
                    return $file;
                }
            }
        }
    }
}

这段代码作用其实寻找 ps4中是否有满足类名的首字母,如果有继续往下找

作用:查找字符串首次出现的位置

file_exists

$logicalPathPsr4 = strtr($class, '\', DIRECTORY_SEPARATOR) . $ext;

$first = $class[0];
if (isset($this->prefixLengthsPsr4[$first])) {
    $subPath = $class;
    while (false !== $lastPos = strrpos($subPath, '\')) {
        $subPath = substr($subPath, 0, $lastPos);
        $search = $subPath . '\';
        if (isset($this->prefixDirsPsr4[$search])) {
            $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
            foreach ($this->prefixDirsPsr4[$search] as $dir) {
                if (file_exists($file = $dir . $pathEnd)) {
                    return $file;
                }
            }
        }
    }
}

这段代码作用其实寻找 ps4中是否有满足类名的首字母,如果有继续往下找

作用:检查文件或目录是否存在