我收到PHP通知错误。这段代码在php 5.3中运行良好,但随后我将PHP升级到PHP 7.我想要做的是,从链接中获取URL,然后只显示随URL附带的参数。这是代码。
的index.php
<?php
require_once('bootstrap.php');
$bootstrap = new Bootstrap($_GET);
?>
bootstrap.php中
<?php
class Bootstrap{
private $controller;
private $action;
private $request;
public function __construct($request){
$this->request = $request;
if($this->request['controller'] == ''){
$this->controller = "Home";
}
elseif($_GET($request['controller'])){
$this->controller = $this->request['controller'];
}
if($this->request['action'] == ''){
$this->action = "index";
} else{
$this->action = $this->request['action'];
}
echo "<br />$this->controller<br />$this->action";
}
?>
转到网址时的输出:localhost / myDir / index.php / abc / def
注意:未定义的索引:第8行的/srv/http/myDir/bootstrap.php中的控制器
注意:未定义的索引:第14行的/srv/http/myDir/bootstrap.php中的操作
主页
索引
答案 0 :(得分:2)
测试empty()
... true
为0,'0',false,'',空数组()
......通知也消失了!
...为你的其他ifs和数组索引做同样的事情!
if(empty($this->request['action'])) {
为了避免类似的警告,您还应该在方法,函数等中提供默认值:
function ($arg=FALSE, $arg2=TRUE, $arg3=5, ...) {
答案 1 :(得分:0)
如果您的代码运行正常&amp;问题只是删除通知错误然后你可以在php脚本中使用error_reporting(0)
。
添加error_reporting(0)
作为php脚本中的第一个语句
答案 2 :(得分:0)
测试是否设置:
isset($this->request['action'])
isset($this->request['controller'])
像这样:
<?php
class Bootstrap{
private $controller;
private $action;
private $request;
public function __construct($request){
$this->request = $request;
foreach ($request as $key => $value) {
echo $key . " = " . $value;
}
if(isset($this->request['controller']) && $this->request['controller'] == ''){
$this->controller = "Home";
}
elseif(isset($this->request['controller']) && $_GET($request['controller'])){
$this->controller = $this->request['controller'];
}
if(isset($this->request['action']) && $this->request['action'] == ''){
$this->action = "index";
}
else{
$this->action = $this->request['action'];
}
echo "<br />$this->controller<br />$this->action";
}
?>