【发布时间】:2015-06-26 19:35:06
【问题描述】:
所以基本上我已经开始使用 OOP,而我根据教程编写的脚本给了我 2 个问题:
当我尝试将对象转换为变量时,出现此错误:
注意:尝试在第 14 行的 /home/stream/V2/templates/homepage.php 中获取非对象的属性
第14行的代码是:
<a href=".action=viewSeries&seriesId=<?php echo $series->id ?>"><?php echo htmlspecialchars( $series->id ) ?></a>
第二次没有将对象转换为变量并尝试直接从中访问数据,我什么也得不到。
我基本上有 3 页来帮助从表中获取数据:
Homepage.php
<?php include( "templates/include/header.php" ); ?>
<ul id="headlines">
<?php
echo $results['totalRows'];
foreach( $results['series'] as $series ){ ?>
<li>
<h2>
<!-- remember to add htmlspecialchars -->
<span class="title"><?php echo $results['series']->description ?></span>
<a href=".action=viewSeries&seriesId=<?php echo $series->id ?>"><?php echo htmlspecialchars( $series->id ) ?></a>
</h2>
</li>
<?php
} ?>
</ul>
<p><a href="./?action=archive">Series Archive</a></p>
<?php include( "templates/include/footer.php" ); ?>
index.php
<?php
require( "core/config.php" );
$action = isset ( $_GET['action'] ) ? $_GET['action'] : "";
switch ( $action ){
case 'series':
series();
break;
case 'viewSeries':
viewSeries();
break;
default:
homepage();
}
function series(){
$results = array();
$data = Series::getList();
$results['series'] = $data['results'];
$results['totalRows'] = $data['totalRows'];
$results['pageTitle'] = "Series Title";
require( TEMPLATE_PATH . "/series.php" );
}
function viewSeries(){
if( !isset( $_GET['seriesId'] ) || !$_GET['seriesId'] ){
homepage();
return;
}
$results = array();
$results['series'] = Series::getBydId( (int)$_GET['seriesId'] );
$results['pageTitle'] = $results['series']->title . "| Sub title";
require( TEMPLATE_PATH . "/viewSeries.php" );
}
function homepage(){
$results = array();
$data = Series::getList( HOMEPAGE_NUM_SERIES );
$results['series'] = $data['results'];
$results['totalRows'] = $data['totalRows'];
$results['pageTitle'] = "Streamaton Homepage";
require( TEMPLATE_PATH . "/homepage.php");
}
这是 Series.php 类中的一段代码:
//Return all (or range of) Series objects in the db
public static function getList( $numRows=100000, $order="id DESC" ){
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "SELECT id, description FROM series ORDER BY :order LIMIT :numRows";
//SQL_CALC_FOUND_ROWS *
$st = $conn->prepare( $sql );
$st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
$st->bindValue( ":order", $order, PDO::PARAM_STR );
$st->execute();
$list = array();
while( $row = $st->fetch() ){
$series = new Series( $row );
$list = $series;
}
//Now get the total number of series that match the criteria
$sql = "SELECT FOUND_ROWS() AS totalRows";
$totalRows = $conn->query( $sql )->fetch();
$conn = null;
return ( array ( "results" => $list, "totalRows" => $totalRows[0] ) );
}
整个 series.php 代码如下:http://pastebin.com/f95A8NPr
【问题讨论】:
-
$results['series']中有什么内容? -
不应该
$results['series']->description是$series->description吗? -
您可能还想在
foreach循环中尝试var_dump($series)看看它是什么样子的。 -
$results是index.php函数中的局部变量。您无法在homepage.php中访问它。另外,我看不到homepage.php包含index.php的位置。 -
我快速查看了您的构造函数的代码,看起来您使用
_construct代替了__construct。除了@Barmar 的答案所建议的更改之外,您是否想更改它并查看它是否有任何不同? php.net/manual/en/language.oop5.decon.php