PHP类提供空对象

时间:2016-04-15 12:47:29

标签: php mysql

我的index.php页面有此代码,默认用户访问主页。

<?php 
require_once ( "config.php" );
$action = isset( $_GET['action'] ) ? $_GET['action'] : "";
switch ( $action ) {
  case 'seeAll':
    seeAll();
    break;
  case 'viewResource':
    viewResource();
    break;
  default:
    homepage();
}

function homepage() {
  $results = array();
  $learnData = Resource::getHomePageTiles( 'learn' );
  $practiceData = Resource::getHomePageTiles( 'practice' );
  $elseData = Resource::getHomePageTiles( 'else' );
  $results['learn'] = $learnData;
  $results['practice'] = $practiceData;
  $results['else'] = $elseData;
  $results['pageTitle'] = "Couch To Code";
  require_once ( "templates/homepage.php" );
}

调用我的Resource类的getHomePageTiles方法。 以下是Resource类和getHomePageTiles方法的代码。

class Resource {
  public $id = null;
  public $title = null;
  public $summary= null;
  public $url = null;
  public $content = null;
  public $category = null;
  public $is_free = null; //????
  public $is_featured = null;
  public $is_favorite = null;
  public $date_created = null;

  public function __construct( $data ) {
    if ( isset ( $data['id'] ) ) $this->id = (int) $data['id'];
    if ( isset ( $data['title'] ) ) $this->title = $data['title'];
    if ( isset ( $data['summary'] ) ) $this->summary = $data['summary'];
    if ( isset ( $data['url'] ) ) $this->url = $data['url'];
    if ( isset ( $data['content'] ) ) $this->content = $data['content'];
    if ( isset ( $data['category'] ) ) $this->category = $data['category'];
    if ( isset ( $data['is_free'] ) ) $this->is_free = (int) $data['is_free'];
    if ( isset ( $data['is_featured'] ) ) $this->is_featured = (int) $data['is_featured'];
    if ( isset ( $data['is_favorite'] ) ) $this->is_favorite = (int) $data['is_favorite'];
    if ( isset ( $data['date_created'] ) ) $this->date_created = date("Y-m-d H:i:s");
}

public static function getHomePageTiles( $type ) {
  global $DB_HOST;
  global $DB_NAME;
  global $DB_USERNAME;
  global $DB_PASSWORD;
  $conn = new mysqli( $DB_HOST, $DB_USERNAME, $DB_PASSWORD, $DB_NAME );
  if (mysqli_connect_errno()) {
    printf("Connection to the database failed: %s/n", $mysqli -> connect_error);
    exit();
  }
  $sql = "SELECT * FROM resources WHERE category = ? ORDER BY position ASC LIMIT 4;";
  if ($st = $conn->prepare( $sql )) {
    $st->bind_param( "s", $type );
    $st->execute();
    $list = array();
    while ( $row = $st->fetch() ) {
        $resource = new Resource( $row );
        $list[] = $resource;
    }
    $conn = null;
    return $list;
  } else {
    printf("Errormessage: %s\n", $conn->error);
  }
}

我的Homepage.php文件中包含以下代码

<?php 
    foreach ( $results['learn'] as $learnTile ) { 
    print_r($learnTile);
?>
    <div class="col-md-3 col-xs-6 featured">

        <a href=".?action=viewResource&amp;resourceId= <?php echo($learnTile->id)?> ">
        <img src="#" class="thumbnail img-responsive">
        </a>
    </div>
<?php } ?>

代码正在拉动正确数量的Resource对象,但是有些东西正在变形,因为我的链接(在homepage.php中)没有从Resource对象中提取id。

所以,调试我打印出来($ learnTile)并得到空对象/数组。

 Resource Object ( [id] => [title] => [summary] => [url] => [content] => [category] => [is_free] => [is_featured] => [is_favorite] => [date_created] => )  Resource Object ( [id] => [title] => [summary] => [url] => [content] ...

我哪里错了?是我的构造函数不工作?显然,Resource对象将原始属性定义中的空值拉为null。 我已经被困在这个问题好几天了,所以我们非常感谢任何帮助/指导。谢谢!

1 个答案:

答案 0 :(得分:1)

$row内部从未有过数组。参见手册:php.net/manual/en/mysqli-stmt.fetch.php它只返回一个布尔值。您应该在使用prepare时绑定结果。