2016-04-08 15 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"; 
    } 
?> 

出力:localhostの/ myDirに/ index.phpを/ ABC/DEF

通知:未定義のインデックス:コントローラ/srv/http/myDir/bootstrap.php o n個のライン8
お知らせ:未定義のインデックス:ライン上の/srv/http/myDir/bootstrap.phpでアクション14

0用trueなりますempty()ホーム
インデックス

+0

[参照の可能性の重複 - このエラーはどういう意味: isset($this->request['action']) isset($this->request['controller'])

このようにPHPで?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) –

答えて

2

テスト...、 '0'、false、 ''、空の配列() ...通知も消えました! ...他のifと配列のインデックスでも同じことをしてください!同様の警告を回避するには

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

、あなたはまた、など、あなたの方法、機能、にデフォルト値を提供する必要があります:

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

ありがとう... :) – Beevk

0

あなたのコードは、罰金&問題が動作している場合にのみ、その後の通知エラーを削除することですあなたはerror_reporting(0) PHPスクリプトで使用することができます。

ISSET場合は、PHPスクリプトの最初のステートメントとして

+0

そのv彼がCやJavaのような他のプログラミング言語に移行しなければならない場合は、厄介な「回避策」を使用してください。それは単にエラー/警告を隠しますが、解決しません。 –

+0

NO!それは悪いプログラミングの習慣です。あなたはオンラインに行くときにそれをすることができます(しかし、とにかくそれらの警告と注意はすべて廃止されるべきです)。 – djot

+0

実際には機能しません。 Homeではなくabcを表示し、indexではdefを表示する必要があります。だから私はエラーが出力に影響を与えていると思います。 – Beevk

0

テストをerror_reporting(0)を追加します。

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