2016-05-05 3 views
0

私はこのエラーが表示されます。set_error_handler()は無効なコールバックを受け取りますか?

set_error_handler() expects the argument (userErrorAdvice) to be a valid callback


trait userErrorAdviceTrait 
{ 
    public function userErrorAdvice() 
    { 
     $error = error_get_last(); 
     $_SESSION['errorStore'] = $error; 
     $errorMessage = "No file exists for the PageController"; 
     if (strstr($error['message'], "No file exists for the PageController class")) 
     { 
      header('Location: http://192.171.127.39/~louis/errorAdvicePage.php?errorType=NoPageControllerError'); 
      exit; 
     } 
    } 
    public function setUserErrorAdvice() 
    { 
     set_error_handler("userErrorAdvice"); 
    } 
} 

use userErrorAdviceTrait; 
public static function makePageController($pageName) 
{ 
    //self::shutDownFunction(); 
    //self::shutdown_function(); 
    self::setUserErrorAdvice(); 

    //Rest of code .... 

これは詳細を書くために私を強制的にですが、私は言うことは何も残っていません。

+0

これは100%ではありませんが、範囲の問題ではないかと推測しています。 'set_error_handler'は特定のクラスではなく、スクリプト全体であるため、' userErrorAdvice() 'というグローバル関数を探しています。それは、それがそのエラーをスローすることがわからないので。 – Mike

+0

'http://192.171.127./~louis [etc]'は有効なURLのようには見えません。 – Mike

+0

nvm URLはstackoverflowでどのように入力したかのエラーです。調整します。 –

答えて

1

私はこのために簡単な作業テストを行いました。ここに私が見直すコードがあります

<?php 

trait userErrorAdviceTrait 
{ 
    public function function userErrorAdvice($errno, $errstr) 
    { 
     echo 'error no '.$errno.' and message '.$errstr.PHP_EOL; 
    } 

    public function setUserErrorAdvice() 
    { 
     set_error_handler("userErrorAdvice"); 
    } 
} 

class TestErrorFunction 
{ 
    use userErrorAdviceTrait; 

    public static function makePageController($pageName) 
    { 
     self::setUserErrorAdvice(); 
    } 
} 

TestErrorFunction::makePageController('page'); 

ここにいくつかの問題があります。説明して修正しましょう。

  1. 静的でない関数を静的関数として使用します。メソッド(ファンクション)userErrorAdvice()を実行する必要がある場合は、「持っている」オブジェクトである必要があります。しかし、そのオブジェクトのオブジェクトは作成されません。

  2. set_error_handler()関数の引数はPHP callableである必要があります。我々の場合、グローバル関数はありませんuserErrorAdvice()

問題を解決する方法はいくつかあります。たとえば、

<?php 

trait userErrorAdviceTrait 
{ 
    public static function userErrorAdvice($errno, $errstr) 
    { 
     echo 'error no '.$errno.' and message '.$errstr.PHP_EOL; 
    } 

    public static function setUserErrorAdvice() 
    { 
     set_error_handler([userErrorAdviceTrait::class, "userErrorAdvice"]); 
     // the other option here 
     // set_error_handler('userErrorAdviceTrait::userErrorAdvice'); 
    } 
} 

class TestErrorFunction 
{ 
    use userErrorAdviceTrait; 

    public static function makePageController($pageName) 
    { 
     self::setUserErrorAdvice(); 
     include 'no existing file'; 
    } 
} 

TestErrorFunction::makePageController('page'); 

このコードは次の出力を生成します。私は呼び出し可能なパラメータを修正

  1. をやっていること

    $ php code.php 
    error no 2 and message include(no existing file): failed to open stream: No such file or directory 
    error no 2 and message include(): Failed opening 'no existing file' for inclusion (include_path='.:/usr/share/php') 
    

    つのより多くの時間。

  2. スタティックな理由として定義されたメソッドは、静的コンテキストで使用されます。
関連する問題