如何在 php 类中使用 $_GET?我试着这样做:
class UploadHandler
{
public $a = $_GET['size'];
}
它总是给我一个错误
尝试 :
class UploadHandler
{
public $a;
function __construct() {
$this->a = isset($_GET['size']) ? $_GET['size'] : null;
}
}
您的目标是获取文件上传的大小吗?如果是这样,您将需要使用 $_FILES 变量。 http://php.net/manual/en/reserved.variables.files.php 具体来说,http://www.php.net/manual/en/features.file-upload.post-method.php
从技术上讲,一个类没有理由不能访问全局变量。但是,最佳实践可能建议您的控制器/网关提取数据并将其传递给类。例如
<?
$uploaded_file = $_FILES['userfile']['tmp_name'];
$UploadHandler = new UploadHandler();
$permanent_local_path = $UploadHandler->saveFileSomewhere($uploaded_file);
?>
尝试这个:
class UploadHandler
{
public $a;
public function __construct(){
if(isset($_GET['size']){
$this->a = $_GET['size'];
}else{
$this->a = null // or maybe 0
}
}
}