【发布时间】:2010-08-05 00:51:07
【问题描述】:
我正在以旧方式编程 - 仍然使用函数而不是类。例如,我在为网站创建基本模板时所做的一些基本操作是将页眉和页脚放在一个函数中,就像在一个名为 functions.php 的文件中一样
<?php
//header
function outline_start() {
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>my site</title>
</head>
<body>
';
}
//footer
function outline_end() {
echo '
</body></html>
';
}
?>
然后在另一个文件中,比如 index.php,我会执行以下操作:
<?php
include(functions.php);
outline_start();
echo 'hello world';
outline_end();
?>
我被告知需要使用全局变量,这可能会带来安全方面的问题,而且通常是不好的做法 - 对吧?
最佳做法是什么?
【问题讨论】:
-
学习 oop。你的生活会感谢你。