【问题标题】:Fatal error: Call to a member function getValueEncoded() on a non-object [closed]致命错误:在非对象上调用成员函数 getValueEncoded() [关闭]
【发布时间】:2012-10-29 20:51:51
【问题描述】:

我有一个文件正在遍历目录层次结构。子目录由员工的 ID 命名。我已经实例化了员工类来获取员工的姓名,这样我就可以输出姓名而不是 ID。

我不断收到错误“致命错误:调用非对象上的成员函数,我不明白为什么。

我可以使用 var_dump() 转储对象并查看所有内容。只有 3 个子目录,每个子目录包含 4 个文件。

下面是我的代码:

             <div id="empDir1" style="padding-left:20px; padding-bottom:25px;">
              <?php
                  $dirPath = "emp_files";
                  function fileManager( $dir ) {

                  $dirParts = explode("/", $dir);
                  $dirId = $dirParts[1];

                  if ( !isset( $dirId ) ){
                      echo "<div></div>";
                  } else {

                  $employeeId = $dirId;
                  $employee = Employee::getEmployee( $employeeId );

                  $emp_lastname = $employee->getValueEncoded('emp_lastname');
                  $emp_firstname = $employee->getValueEncoded('emp_firstname');

                  $dirListing = $emp_lastname . ', ' . $emp_firstname;
                  }

                    echo "<h2>File Listing for $dirListing ...</h2>";
                      if ( !( $handle = opendir( $dir ) ) ) die( "Cannot open $dir." );

                    $fileImage = array(
                        'doc' => '../../images/folder-doc.png',
                        'txt' => '../../images/folder-txt.png',
                        'msg' => '../../images/folder-msg.png',
                        'pdf' => '../../images/folder-pdf.png',
                        'png' => '../../images/folder-png.png',
                        'jpg' => '../../images/folder-jpg.png',
                        'bmp' => '../../images/folder-bmp.png',
                        'gif' => '../../images/folder-gif.png',
                        'xls' => '../../images/folder-xls.png',

                        );


                    $files = array();

                    while ( $file = readdir( $handle ) ) {
                      if ( $file != "." && $file != ".." ) {
                        if ( is_dir( $dir . "/" . $file ) ) $file .= "/";
                        $files[] = $file;
                      }
                    }

                    sort( $files );
                      echo '<table>';
                      foreach ( $files as $file ) {
                        $fileParts = explode('.', $file);
                        $fileExt = strtolower($fileParts[count($fileParts) -1]);
                        echo '<tr>';
                        echo '<td width="650px" class="row2">';
                        echo ("<a class='ptext' href=" . $dir . "/" . $file . ">" . ( isset($fileImage[$fileExt] ) ? "<img src='". $fileImage[$fileExt] . "' />" : "<img src='../../images/folder-unknown.png' />" ) . " $file</a><a style='float:right;padding-right:10px;' href='empUnlink.php?filePath=" . $dir . $file ."' id='" . $dir . $file ."' class='deleteFile'>x</a></p>"); 
                        echo '</td>';
                        echo '</tr>';
                      }
                      echo '</table>';

                    foreach ( $files as $file ) {
                      if ( substr( $file, -1 )  == "/" ) fileManager( "$dir/" . substr( $file, 0, -1 ) );
                    }


                    closedir( $handle );
                  }

                  fileManager( $dirPath );
              ?>

【问题讨论】:

  • 你能展示getValueEncoded方法吗?

标签: php class pdo traversal


【解决方案1】:

在不知道 Employee 类的代码是什么的情况下,我的猜测是静态函数 Employee::getEmployee() 没有找到与 $employeeid 匹配的员工,并返回 null。在尝试使用之前检查$employee 是否为空。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 2013-11-25
    • 2012-05-30
    • 2016-08-30
    • 2015-02-06
    相关资源
    最近更新 更多