-1

我以为我可以直接调用一个方法,classname::method但它没有用。这怎么可能?我用 xdebug 跟踪了函数调用,但它给了我一个错误。当方法需要一些类变量时会不会有问题?

这有效:

    $a = new export_csv();
    $a->dl_csv();

这不:export_csv::dl_csv();

我在我的方法中添加了公共静态无济于事。我在日志文件中没有得到任何东西,并且 xdebug 停止工作而没有错误消息?这是正常的吗?我的课程包括一些课程并连接到我的数据库并提取和回显一些行?

 require_once(PATH_t3lib.'class.t3lib_db.php');
 require_once(PATH_t3lib.'class.t3lib_div.php');
 require_once(PATH_t3lib.'utility/class.t3lib_utility_math.php');

 class export_csv 
 {
     var $filename = 'meinname.csv';

     public static function dl_csv()
     {
        // bitte nicht ändern muß zur laufzeit geladen werden
        include(PATH_typo3conf.'localconf.php');

        $GLOBALS['TYPO3_DB'] = t3lib_div::makeInstance('t3lib_DB');
        $GLOBALS['TYPO3_DB']->connectDB($typo_db_host, $typo_db_username,   
            $typo_db_password, $typo_db);

        session_start();
            $targetCat = mysql_real_escape_string($_SESSION['targetCat']);

            // calculate the number of rows for the query. We need this for paging the 
            result
            $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(  'V.title VT, V.uid VU,  
            S.title ST, S.uid SU, S1.uid S1U, S1.title S1T',
                                                    'tx_category V
                                                    INNER JOIN tx_category S ON  

                                                    V.parent_category=S.uid
                                                    INNER JOIN tx_category S1 ON 
                                                    S.parent_category=S1.uid',
                                                    'V.uid='.$targetCat.' OR S.uid='.
                                                     $targetCat.' OR S1.uid='.
                                                     $targetCat.
                                                    ' AND V.deleted=0 AND V.hidden=0',
                                                    '',
                                                    '',
                                                    ''
                            );                      
    $arrcount=0;
    while ( $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res) )
    {
        $categories[] = mysql_real_escape_string($row["VU"]);
        ++$arrcount;
    }

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Type:text/comma-separated-values");
    header("Content-Transfer-Encoding: binary");
    header("Content-Disposition: attachment; filename=$this->filename");

    if ( $arrcount )
    {
        $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*',
                                                      'tx_download',
                                                      'category IN('.implode(",",
                                                      $categories).")",
                                                      '',
                                                      "",
     ""                                                  
                                                    );
        while ($row = mysql_fetch_assoc($res) )
        {
            $s = array ();
            $s[] = $row['1'];
            $s[] = $row['2'];
            $s[] = $row['3'];
            $s[] = $row['4'];
            $s[] = $row['5'];

            echo '"'. substr ( implode ( '","', $s ),0,strlen ( implode ( '","', $s) ) - 2  ) . "\r\n";

        }       
    }
  }
 }
4

2 回答 2

2

定义为 的方法static,因此不能使用实例属性/方法(通过$this),可以通过以下方式静态调用:

ClassName::methodname();

// Static method definition:
public static function methodname() {
  echo "I'm the method, I don't use \$this in any way!";
  // Any attempt to use $this in here would result in error.
}

查看有关static方法和属性的 PHP 文档

看到模式代码后更新:

您的dl_csv()函数$this->filename在输出标头中访问,这在静态调用中是无效的。

// Can't do this!
header("Content-Disposition: attachment; filename=$this->filename");

相反,您还需要将 定义$filename为静态属性并通过以下方式调用self::

// Top of the class:
// Instead of the old PHP4 var keyword, define $filename as a public static property
public static $filename = 'meinname.csv';

// Later...
// Inside your method header() call:
header("Content-Disposition: attachment; filename=" . self::$filename);
于 2012-07-27T13:56:09.063 回答
0

它是Class::method();,但是我真的很好奇为什么你认为你需要它在整个应用程序中都可以访问(globally)。

您可能对依赖注入感兴趣。

于 2012-07-27T13:57:37.183 回答