我正在尝试将$url = $_POST['url'];
设置为类中的公共属性,但编辑器不会让我这样做。这是为什么?以下是我的代码:
class insertAd {
public $uploadOk = 0;
public $newVar1 = 0;
我希望:$url = $_POST['url'];
function chkEmptyBoxes() {
$url = $_POST['url'];
$start_time = (int) $_POST['start_time'];
$end_time = (int) $_POST['end_time'];
$arr = array("Url" => "$url", "Start Time" => "$start_time", "End Time" => "$end_time");
foreach ($arr as $key => $val) {
if (empty($val)) {
echo "<b>" . $key . "</b>" . " " . "require: ";
$this->uploadOk = 0;
} else {
$this->uploadOk = 1;
$this->newVar1 = 1;
}
}
}
}
答案 0 :(得分:2)
正如phplover所说,你需要设置变量。
在你的例子中:
class insertAd {
public $uploadOk = 0;
public $newVar1 = 0;
public $url;
// When you instantiate the class, set the $url to the specific value.
public function __construct() {
/* check, if the url's matches a specific pattern and it is a valid url
* Better safe than sorry.
*/
if(filter_var($_POST['url'], FILTER_VALIDATE_URL))
{
{
$this->url = $_POST['url'];
}
}
}
function chkEmptyBoxes() {
$start_time = (int) $_POST['start_time'];
$end_time = (int) $_POST['end_time'];
$arr = array("Url" => $this->url, "Start Time" => "$start_time", "EndTime" => "$end_time");
foreach ($arr as $key => $val) {
if (empty($val)) {
echo "<b>" . $key . "</b>" . " " . "require: ";
$this->uploadOk = 0;
}
else {
$this->uploadOk = 1;
$this->newVar1 = 1;
}
}
}
}
请记住,您要清理user_input!这是非常重要的事情。不要依赖用户的理智。
答案 1 :(得分:0)
我知道你想从类函数中为全局变量$url
赋值。试试这个:
...
function chkEmptyBoxes() {
$GLOBALS['url'] = $_POST['url'];
$start_time = (int) $_POST['start_time'];
...
如果它不起作用,请在调用此类函数之前将$ url设置为全局范围的初始值。