Setter trouble with empty function .......

liunx

Guest
Hi.
I don't see the point :(

<?php
class InvalidArgException extends Exception{
public function __construct($message, $code = 0){
parent::__construct($message, $code);
}
public function __toString(){
return " ".__CLASS__ . "<br /> Line : <b>[{$this->line}]</b><br /> Code :<b>[{$this->code}]</b><br /> Msg : {$this->message}<br /> File : <b>{$this->file}</b>\n";
}
}
class Files{
protected $files= array(
'name'=>'',
'type'=>'',
'tmp_name'=>'',
'error'=>0,
'size'=>'',

);
public function __construct($inputName='upfile'){
if(!isset($_FILES[$inputName])){
throw new InvalidArgException('Invalid parameter.Invalid key for $_FILES in class <b>['.__CLASS__.']</b>');
}
$this->name= $_FILES[$inputName]['name'];
$this->type= $_FILES[$inputName]['type'];
$this->tmp_name= $_FILES[$inputName]['tmp_name'];
$this->error= $_FILES[$inputName]['error'];
$this->size= $_FILES[$inputName]['size'];
}
protected function __set($nm, $val){
if(!isset($this->files[$nm])){
throw new InvalidArgException('Invalid parameter.The referenced element is not valid<b>['.__CLASS__.']</b>');
}
$this->files[$nm]= $val;
}
protected function __get($nm){
if(!isset($this->files[$nm])){
throw new InvalidArgException('Invalid parameter.The referenced element is not valid<b>['.__CLASS__.']</b>');
}
return $this->files[$nm];
}
}
class Upload extends Files{
public function __construct($inputName='upfile'){
parent::__construct($inputName);
var_dump($this->name);
var_dump(empty($this->name));//true
$tmp= $this->name;
var_dump(empty($tmp));//false
}
}
if(isset($_POST['test'])){
$o= new Upload('upfile');
echo "<pre>";
print_r($o);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form id="myform" action="due.php" method="post" enctype="multipart/form-data">
<input type="text" name="test"><br />
<input type="file" name="upfile"><br />
<input type="hidden" name="MAX_FILE_SIZE" value="10000">
<input type="submit" value="Invia il file">
</form>
</body>
</html>



In a nutshell I want to check if a file was
really chose and than throw an Exception or
caught an error but I don't see the point of
empty function behaviour.
Could you help me, please ?

Bye.I don't see the empty function. But

function checkUploadExists()
{
if(empty($_FILES['upfile']))
throw new InvalidArgException('....');
}
is perfectly reasonable. If the field is empty, an exception is thrown; if it's not, then there is nothing for the function to do, so it does nothing.I don't see the empty function. But

function checkUploadExists()
{
if(empty($_FILES['upfile']))
throw new InvalidArgException('....');
}
is perfectly reasonable. If the field is empty, an exception is thrown; if it's not, then there is nothing for the function to do, so it does nothing.

Thanks for the point. But I've asked other stuff
I point out to you :

class Upload extends Files{
public function __construct($inputName='upfile'){
parent::__construct($inputName);
var_dump($this->name);
var_dump(empty($this->name));//true
$tmp= $this->name;
var_dump(empty($tmp));//false
}
}


Well, I don't see why if I check
directly var_dump(empty($this->name))
I got a true value (while it is not empty
var_dump($this->name)) and if I assign
it to a temporary var I got a false value ???


Bye.
 
Back
Top