最佳实践:将 PHP 代码放在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6874169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best practice: where to put the PHP code?
提问by the_source
I do admit this question is going to be a bit vague, but I will try to explain what I'm trying to accomplish by a few examples. I have some PHP code that loads a bunch of variables from the MySQL database, contains some declarations, some functions to quickly output HTML code etc. However I would love to do all that stuff before anything is sent to the client.
我承认这个问题会有点模糊,但我会尝试通过几个例子来解释我想要完成的事情。我有一些 PHP 代码,它从 MySQL 数据库加载一堆变量,包含一些声明,一些快速输出 HTML 代码的函数等。但是我很乐意在将任何内容发送到客户端之前完成所有这些工作。
So I do:
所以我这样做:
<?php
include("somefile.inc");
function bla()
{
...
}
if (fails)
echo "Error: ...<br />";
?>
<!DOCTYPE>
<html>
<head>
<script>
...
<?php echo $someString; ?>
...
</script>
</head>
<body>
...
</body>
</html>
This is all fine and ok, until I get an error. The echo will not show in the browser because it's before all HTML... So I modified:
这一切都很好,直到出现错误为止。echo 不会显示在浏览器中,因为它在所有 HTML 之前......所以我修改了:
<!DOCTYPE>
<html>
<head>
<script>
...
<?php echo $someString; ?>
...
</script>
</head>
<body>
<div class="error_block">
<?php
include("somefile.inc");
function bla()
{
...
}
if (fails)
echo "Error: ...<br />";
?>
</div>
...
</body>
</html>
Now I can actually see errors, which is good. But now the problem arises that in the header, or scrips, I cannot access variables that will be loaded later on in the newly created error_block.
现在我实际上可以看到错误,这很好。但是现在出现的问题是,在标题或脚本中,我无法访问稍后将在新创建的 error_block 中加载的变量。
I really don't like splitting the code in the error_clock to some above the HTML document and some in the error_block. And I also don't want to use PHP's die() function which abrubtly ends the execution.
我真的不喜欢将 error_clock 中的代码拆分为 HTML 文档上方的一些代码和 error_block 中的一些代码。而且我也不想使用突然结束执行的 PHP 的 die() 函数。
Anyone can give their 2 cents on this issue? Thanks.
任何人都可以在这个问题上付出 2 美分吗?谢谢。
采纳答案by Phil
If you're looking for an alternate solution, I have one for you. What I like doing is having the logic in before the DOCTYPE
如果您正在寻找替代解决方案,我为您准备了一个。我喜欢做的是在逻辑之前DOCTYPE
if(error) { $error = "Please do something" }
Than, down in the document I have a div just for the error (Thanks @Dave for the input)
然后,在文档中我有一个 div 只是为了错误(感谢@Dave 的输入)
<?php echo $error != '' ? '<div id="error">' . $error . '</div>' : ''; ?>
This div will not appear if there isn't an error (meaning $error
is empty) and it makes it easier for you to style the error message the way you would like
如果没有错误(意思$error
是空的),这个 div 不会出现,它让你更容易按照你想要的方式设置错误消息的样式
#error { color:red; }
If you want to get fancy, you can use some jQuery to hide/show the div so that the error doesn't have to persist.
如果你想花哨,你可以使用一些 jQuery 来隐藏/显示 div,这样错误就不必持续存在。
$('#error').show().delay(7000).fadeOut();
回答by marklu
You should look into using try-catch blocks and generating exceptions if you want to do some post-processing with the error message, which includes display.
如果您想对错误消息(包括显示)进行一些后处理,您应该考虑使用 try-catch 块并生成异常。
回答by Ryan
What is often forgotten is that PHP is an INLINE programming language in essence, this means it is designed to be processed by the server as the server reads down the page, and with this it is designed to be split up into chunks. Recently OOP (Object Oriented Programming) has been introduced to PHP making it more flexible.
经常被遗忘的是,PHP 本质上是一种内联编程语言,这意味着它被设计为在服务器读取页面时由服务器处理,因此它被设计为分成块。最近 OOP(面向对象编程)被引入到 PHP 中,使其更加灵活。
So with this information in hand I would take the OOP path in this case and do something like:
因此,有了这些信息,在这种情况下,我将采用 OOP 路径并执行以下操作:
<!DOCTYPE>
<?php
include("somefile.inc");
function bla()
{
...
}
function failureError($code){
if(!empty($code)) ...
}
if ($a = $b) {
code goes here
} else {
$code = 'error123';
}
?>
<html>
<head>
<script>
...
<?php failed($code); ?>
...
</script>
</head>
<body>
...
</body>
</html>
By writing using functions you can cut down your development time and group the majority of your code just calling what you need when you need it.
通过使用函数编写,您可以减少开发时间并将大部分代码分组,只需在需要时调用所需内容即可。
Another way of declaring your error class(es)/functions to help with server response time is to do something like:
声明错误类/函数以帮助缩短服务器响应时间的另一种方法是执行以下操作:
if ($a = $b) {
code goes here
} else {
include("errorStuff.php");
}
This will only include the error class(es)/functions when an error is encountered.
这将仅包括遇到错误时的错误类/函数。
Just remember when you're writing PHP with OOP techniques like this that the server will take longer to process the script than if you write inline. The biggest advantage to an OOP basis is it will cut down your development time and if done correctly it will make it easier to administer future updates to your script.
请记住,当您使用这样的 OOP 技术编写 PHP 时,服务器处理脚本所需的时间比您编写内联的时间长。以 OOP 为基础的最大优势是它会减少您的开发时间,如果正确完成,它将更容易管理您的脚本的未来更新。