2016-04-08 138 views
2

我收到PHP通知错误。这段代码在php 5.3中工作正常,但后来我将PHP升级到了PHP 7.我想要做的是从链接中获取URL,并显示URL附带的参数。这是代码。如何解决此PHP通知错误?

的index.php

<?php 
    require_once('bootstrap.php'); 
    $bootstrap = new Bootstrap($_GET); 
?> 

bootstrap.php中要去URL

<?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"; 
    } 
?> 

输出:本地主机/ MYDIR/index.php文件/ ABC/DEF

注意:未定义的索引:/srv/http/myDir/bootstrap.php中的控制器N线8
注意:未定义指数:行动/srv/http/myDir/bootstrap.php线14

首页
指数

+0

[符号的可能的复制 - 这个错误是什么意思在PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) –

回答

2

试验empty() ...这将是true为0, '0',false,'',空数组() ...并且通知也消失了! ...对你的其他ifs和数组索引也一样!

if(empty($this->request['action'])) { 

为了避免类似的警告,你也应该在你的方法,功能等提供一个默认值:

function ($arg=FALSE, $arg2=TRUE, $arg3=5, ...) { 
+0

谢谢... :) – Beevk

0

如果你的代码工作正常&问题只是删除通知错误,那么你可以在php脚本中使用error_reporting(0)

添加error_reporting(0)作为第一条语句在你的PHP脚本

+0

其v如果他需要迁移到其他编程语言(如C或Java),那么这种解决方法是非常糟糕的。它只是隐藏错误/警告,但不能解决它。 –

+0

不!这是糟糕的编程习惯。上网时可以这样做(但是,所有这些警告和通知都应该过时) – djot

+0

实际上它不起作用。它应该显示abc而不是Home,而不是索引。所以我想错误也会影响输出。 – Beevk

0

测试是否isset: 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"; 
    } 
?>