2016-09-16 19 views
0

我剛剛開始將OOP和PSR-4自動加載類文件學習到我的項目中。目前爲止這麼好,但我依靠FirePHP。 FirePHP工作在我的主要劇本很好,但如果我嘗試在類文件中使用FirePHP不會在所有的工作:如何在class.php文件中使用FirePHP?

<?php namespace App\cmd; 

class help 
{ 
    public $test = "Test successful!"; 
    function __construct() { 
     FB::info('HELP CLASS WAS CALLED!'); 
    } 
} 

我已經試過各種方法,試圖使其工作;試圖包含fb.php,而不是試圖在error_handler中添加ob_start(),而不是。似乎沒有任何工作。

<?php namespace App\cmd; 
ob_start(); 
require_once($_SERVER['DOCUMENT_ROOT'].'/FirePHPCore/fb.php'); 
set_error_handler('myErrorHandler'); 
set_exception_handler('myExceptionHandler'); 

function myErrorHandler($errno, $errstr, $errfile, $errline) 
    {FB::error($errstr, 'Error number' . $errno . ' in ' . $errfile . ' at ' . $errline);} 

function myExceptionHandler($errno, $errstr, $errfile, $errline) 
    {FB::error($errstr, 'Exception number' . $errno . ' in ' . $errfile . ' at ' . $errline);} 

class help 
{ 
    public $test = "Test successful!"; 
    function __construct() { 
     FB::info('HELP CLASS WAS CALLED!'); 
    } 
} 

我得到的錯誤是:

PHP Parse error: syntax error, unexpected 'FB' (T_STRING), expecting function (T_FUNCTION) in help.php 

或者

PHP Fatal error: Class 'App\cmd\FB' not found in help.php 

我必須做一些非常愚蠢的,你能告訴我什麼,我需要做的在課堂上使用FirePHP文件?

+0

請嘗試使用\ FB :: info,因爲您在App \ cmd命名空間中的類內。 – xangxiong

+0

這是一個命名空間的事情。你可以嘗試添加'use FB;'或'\ FB :: info()' – Blake

+0

非常感謝,就是這樣! – Tubusy

回答

0

評論是正確的,謝謝。我只需要正確地命名空間FirePHP:

<?php namespace App\cmd; 
use \FB as FB; 
class help 
{ 
    public $prop1 = "Command test successful!"; 
    function __construct() { 
    FB::warn('HELP CLASS WAS CALLED!'); 
    } 
}