2-1文件封装创建文件的函数出现报错
来源:2-1 文件常用操作函数封装
青春染指现实
2018-04-24 16:35:28
<?php
/**
* [create_file 创建文件操作]
* @param string $filename [文件名]
* @return [boolean] [true| false]
*/
function create_file(string $filename){
//检测文件是否存在,不存在则创建
if(file_exists($filename)){
return false;
}
//检测目录是否存在,不存在则创建
if(!file_exists(dirname($filename))){
//创建目录,可以创建多级
mkdir(dirname($filename),0777,true);
}
// if(touch($filename)){
// return true;
// }
// return false;
if(file_put_contents($filename,'')!==false){
return true;
}
return false;
}
// var_dump(create_file('2.txt'));
var_dump(create_file('a/4.txt'));
// function del_file
?>
报错信息Catchable fatal error: Argument 1 passed to create_file() must be an instance of string, string given, called in C:\AppServ\www\mukephp\chapter2\file.func.php on line 27 and defined in C:\AppServ\www\mukephp\chapter2\file.func.php on line 7
1回答
imooc_澈
2018-04-24
你好,这个报错是因为版本的原因,PHP7版本才支持函数参数的类型声明,将你的版本调整至7或以上,或者将相关的函数参数类型去掉,例如,把下图中的string去掉,只写
function create_file($filename)就可以了。

如果解决了你的问题,请采纳,祝学习愉快~
相似问题