我面臨一個問題,我想讓類頁面知道變量'$格式'。獲取類知道一個變量
// class1.php
<?php
include('./class2.php');
echo $format->getTest(); // returns :-) (declared in class2.php)
class Page {
PUBLIC function getText() {
return $format->getTest(); // returns Call to a member function getTest() on null
}
}
$page = new Page;
?>
// class2.php
<?php
class Format {
PUBLIC function getTest() {
return ":-)";
}
}
$format = new Format;
?>
任何建議/想法?
編輯:
我找到了一種方法:return $GLOBALS['format']->getTest();
但我不喜歡它,它的這麼多的類型。任何其他方式?
菲利普
['$頁=新頁($格式);'](HTTP: //php.net/manual/en/la nguage.oop5.dep.php) – PeeHaa 2015-02-23 18:59:08
這是如何使''格式'已知'頁面? 反正,我得到更多的類不僅僅是'格式',有沒有使用構造函數的另一種方式?我的意思是,我已經在'class1.php'中獲得了varibale,還有什麼方法可以進入'class Page'中? – Philip 2015-02-23 19:01:15