Home > Backend Development > PHP Tutorial > A question about the Upload class in thinkphp3.2.3 version?

A question about the Upload class in thinkphp3.2.3 version?

WBOY
Release: 2016-08-04 09:22:05
Original
1151 people have browsed it

<code>private $config = array(
        'mimes'         =>  array(), //允许上传的文件MiMe类型
        'maxSize'       =>  0, //上传的文件大小限制 (0-不做限制)
        'exts'          =>  array(), //允许上传的文件后缀
        'autoSub'       =>  true, //自动子目录保存文件
        'subName'       =>  array('date', 'Y-m-d'), //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组
        'rootPath'      =>  './Uploads/', //保存根路径
        'savePath'      =>  '', //保存路径
        'saveName'      =>  array('uniqid', ''), //上传文件命名规则,[0]-函数名,[1]-参数,多个参数使用数组
        'saveExt'       =>  '', //文件保存后缀,空则使用原后缀
        'replace'       =>  false, //存在同名是否覆盖
        'hash'          =>  true, //是否生成hash编码
        'callback'      =>  false, //检测文件是否存在回调,如果存在返回文件信息数组
        'driver'        =>  '', // 文件上传驱动
        'driverConfig'  =>  array(), // 上传驱动配置
    );
     public function __construct($config = array(), $driver = '', $driverConfig = null){
        /* 获取配置 */
        $this->config   =   array_merge($this->config, $config);
        
     }</code>
Copy after login
Copy after login

This class puts all the configurations in this array attribute during initialization, but when I read the official documentation, I can set these configurations by calling class attributes, such as:

<code>$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize   =     3145728 ;// 设置附件上传大小

    </code>
Copy after login
Copy after login

I don’t understand when I see this. This maxSize is just a subscript in the config array attribute. Logically speaking, it should be assigned like $upload->config['maxSize']. How can it be assigned directly as an attribute? ?

Reply content:

<code>private $config = array(
        'mimes'         =>  array(), //允许上传的文件MiMe类型
        'maxSize'       =>  0, //上传的文件大小限制 (0-不做限制)
        'exts'          =>  array(), //允许上传的文件后缀
        'autoSub'       =>  true, //自动子目录保存文件
        'subName'       =>  array('date', 'Y-m-d'), //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组
        'rootPath'      =>  './Uploads/', //保存根路径
        'savePath'      =>  '', //保存路径
        'saveName'      =>  array('uniqid', ''), //上传文件命名规则,[0]-函数名,[1]-参数,多个参数使用数组
        'saveExt'       =>  '', //文件保存后缀,空则使用原后缀
        'replace'       =>  false, //存在同名是否覆盖
        'hash'          =>  true, //是否生成hash编码
        'callback'      =>  false, //检测文件是否存在回调,如果存在返回文件信息数组
        'driver'        =>  '', // 文件上传驱动
        'driverConfig'  =>  array(), // 上传驱动配置
    );
     public function __construct($config = array(), $driver = '', $driverConfig = null){
        /* 获取配置 */
        $this->config   =   array_merge($this->config, $config);
        
     }</code>
Copy after login
Copy after login

This class puts all the configurations in this array attribute during initialization, but when I read the official documentation, I can set these configurations by calling class attributes, such as:

<code>$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize   =     3145728 ;// 设置附件上传大小

    </code>
Copy after login
Copy after login

I don’t understand when I see this. This maxSize is just a subscript in the config array attribute. Logically speaking, it should be assigned like $upload->config['maxSize']. How can it be assigned directly as an attribute? ?

Magic method __set(), __set() will be called when assigning values ​​to inaccessible properties.

Look at the __set() method in the class...

<code>/**
 * 使用 $this->name 获取配置
 * @param  string $name 配置名称
 * @return multitype    配置值
 */
public function __get($name)
{
    return $this->config[$name];
}

public function __set($name, $value)
{
    if (isset($this->config[$name])) {
        $this->config[$name] = $value;
        if ('driverConfig' == $name) {
            //改变驱动配置后重置上传驱动
            //注意:必须选改变驱动然后再改变驱动配置
            $this->setDriver();
        }
    }
}</code>
Copy after login

Look, these two magic methods re-implement the access and operation of class attributes;
In this way, it seems that the class attribute $config is an array, but in fact, each element in the class attribute $config of array type can be used through these two magic methods. The respective KEY is used as the class attribute name, and VALUE is used as the class attribute value.

If you don’t rewrite those two magic methods, it may be clear at a glance if you write them in another way:

<code>/**
 * 默认上传配置
 * @var array
 */
$mimes = array(); //允许上传的文件MiMe类型
$maxSize = 0; //上传的文件大小限制 (0-不做限制)
$exts = array(); //允许上传的文件后缀
$autoSub = true; //自动子目录保存文件
$subName = array('date', 'Y-m-d'); //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组
$rootPath = './Uploads/'; //保存根路径
$savePath = ''; //保存路径
$saveName = array('uniqid', ''); //上传文件命名规则,[0]-函数名,[1]-参数,多个参数使用数组
$saveExt = ''; //文件保存后缀,空则使用原后缀
$replace = false; //存在同名是否覆盖
$hash = true; //是否生成hash编码
$callback = false; //检测文件是否存在回调,如果存在返回文件信息数组
$driver = ''; // 文件上传驱动
$driverConfig = array(); // 上传驱动配置</code>
Copy after login

It’s just that this also involves the particularity of the $driverConfig class attribute, which will be discussed separately. Because it is outside the scope of your question.

Related labels:
php
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