我有一個靜態的View
類從另一個類傳遞一個字符串。當字符串作爲變量傳遞時,它可以工作。當我將其更改爲一個恆定的誤差爲:PHP連續變量與常量
[17-FEB-2016 19時08分48秒歐洲/柏林] PHP的警告:包括():無法 開口 「/應用/ MAMP/htdocs中/應用程序/ MAMP/bin/php/php7.0.0/lib/php'中的/in_vegan/ scripts/back_end/views/template' (include_path ='。:/ Applications/MAMP/bin/php'腳本/ back_end /視圖/上 線view.php 23
class View {
/**
* -------------------------------------
* Render a Template.
* -------------------------------------
*
* @param $filePath - include path to the template.
* @param null $viewData - any data to be used within the template.
* @return string -
*
*/
public static function render($filePath, $viewData = null) {
// Was any data sent through?
($viewData) ? extract($viewData) : null;
ob_start();
include ($filePath);// error on this line
$template = ob_get_contents();
ob_end_clean();
return $template;
}
}
class CountrySelect {
const template = 'select_template.php'; //the const is template
public static function display() {
if (class_exists('View')) {
// Get the full path to the template file.
$templatePath = dirname(__FILE__) . '/' . template; //the const is template
$viewData = array(
"options" => '_countries',
"optionsText" => 'name',
"optionsValue" => 'geonameId',
"value" => 'selectedCountry',
"caption" => 'Country'
);
// Return the rendered HTML
return View::render($templatePath, $viewData);
}
else {
return "You are trying to render a template, but we can't find the View Class";
}
}
}
在CountrySelect中做了什麼工作:
$templatePath = dirname(__FILE__) . '/' . static::$template;
爲什麼模板必須是靜態的?我可以使它成爲一個靜態常量嗎?
您需要參考其使用'自:: constname' – PeeHaa
訪問通過雙冒號'做一流的常量::'運營商。 (AKA示波器解析運算符)。如果'static'關鍵字困擾你,那麼另一個選擇就是'self'。關於該主題的[文檔可以略述](http://php.net/manual/en/language.oop5.late-static-bindings.php)。 – Crackertastic