php7编程实战学习笔记(一)

1.注意使用命名空间和自动加载

<?php
namespace Application\Autoload;
class Loader
{
    const UNABLE_TO_LOAD = "";
    static $dirs = array();
    static $registered = 0;
    protected static function loadFile($file)
    {
        if (file_exists($file)) {
            require_once $file;
            return true;
        }
        return false;
    }
    public static function autoLoad($class)
    {
        $success = false;
        $fn = str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
        foreach (self::$dirs as $start) {
            $file = $start . DIRECTORY_SEPARATOR . $fn;
            if (self::loadFile($file)) {
                $success = true;
                break;
            }
        }
        if (!$success) {
            if (!self::loadFile(__DIR__ . DIRECTORY_SEPARATOR . $fn)) {
                throw new \Exception(self::UNABLE_TO_LOAD . ' ' . $class);
            }
        }
        return $success;
    }
    public static function addDirs($dirs)
    {
        if (is_array($dirs)) {
            self::$dirs = array_merge(self::$dirs, $dirs);
        } else {
            self::$dirs[] = $dirs;
        }
    }
    public static function init($dirs = array())
    {
        if ($dirs) {
            self::addDirs($dirs);
        }
        if (self::$registered == 0) {
            spl_autoload_register(__CLASS__ . '::autoload');
            self::$registered++;
        }
    }
    public function __construct($dirs = array())
    {
        self::init($dirs);
    }
}

2.curl和file_get_contents都可以爬取网页数据,curl可以测试接口,请求参数。也可以用DOMDocument类将网站的内容加载到该对象中。

3.phpunit用于测试

猜你喜欢

转载自blog.csdn.net/xiaoxinshuaiga/article/details/81591003