2
我有一個非常簡單的類呼叫從包含的文件類的方法,可以設置類變量而不是調用方法/函數
if(!isset($_GET['page'])) {
$_GET['page'] = "home";
}
class be_site {
var $thelink;
public function get_static_content($page) {
$this->check_path($page);
} // end function
private function check_path($pathfile) {
if(file_exists($pathfile)) {
$b = 1;
include_once($pathfile);
} else {
$b = 2;
include_once('error_page.php');
}
}// End Function
public function selectedurl($subpage, $linkname){
if($subpage == $this->thelink) {
echo "<strong>" . $linkname . "</strong>";
} else {
echo $linkname;
}// End if
} // End function
} /// End site class
現在我在索引中創建新對象.PHP
include('connections/functions.php'); $site_object = new be_site;
在內容都是我
//get file
if(isset($_GET['subpage'])){
$site_object->get_static_content('content/' . $_GET['subpage'] . '.php');
}else {
$berkeley_object->get_static_content('content/' . $_GET['page'] . '.php');
}
好,所以一切正常。但是如果一個被包含的頁面被調用,我會嘗試使用我的其他方法來包裝一個鏈接,如果根據$ _GET ['page']值選擇它,則將其設爲粗體。
例如
<ul>
<li><a href="index.php?page=team&subpage=about" target="_self" title="opens in same window" >
<?php $site_object->thelink = "about_us";
$site_object->selectedurl($_GET['subpage'],'about Our Website'); ?>
</a>
</li>...
等了各個環節。 現在可以在對象中設置變量但不調用該方法。我得到的錯誤
Fatal error: Call to undefined method stdClass::selectedurl()
很奇怪爲什麼我能夠從一個包含文件中設置班上$ thelink變量,但不叫公共職能?
感謝
啊是的。 DOH!我忘了我不得不在全球範圍內聲明它,因爲即使包含的文件現在已成爲它的一部分,它仍然在範圍之外。 – Nobby
抱歉忘了說謝謝:o) – Nobby