简体   繁体   English

什么是php5类的方法呢?

[英]What is a php5 classes approach to this?

I am programming in an old fashion - still using functions and not classes. 我以一种旧的方式进行编程-仍然使用函数而不是类。 For example, something basic I do when creating a basic template for a site is put the header and footer in a function like so in a file called functions.php 例如,在为网站创建基本模板时,我要做的一些基本操作是将页眉和页脚放在函数中,例如在名为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>
';

}
?>

Then in another file, like index.php I'd do the following: 然后在另一个文件(如index.php)中,执行以下操作:

<?php

include(functions.php);

outline_start();

echo 'hello world';

outline_end();

?>

This can pose issues security wise with needing to use global variables I've been told and generally is bad practice - right? 这会给安全问题带来麻烦,因为需要使用我已经被告知的全局变量,通常这是一种不好的做法-对吗?

What is the best practice? 最佳做法是什么?

Best practices can be best determined by the team you're working with. 最佳实践可由与您一起工作的团队确定。 The way I see it, you've got one of two options: 从我的角度来看,您有两种选择之一:

You can prefix your functions into a sort of "the functions with these prefixes are for these operations". 您可以将函数加上前缀“带有这些前缀的函数用于这些操作”。 This is similar to what PHP has been doing for a while, ie db_connect() and db_query(). 这类似于PHP一段时间以来所做的事情,即db_connect()和db_query()。 But really, this is very similar to exactly what you already have (outline_start() and outline_end()). 但实际上,这与您已经拥有的东西(outline_start()和outline_end())非常相似。

If you want to encapsulate this in a class, you can: 如果要将其封装在类中,则可以:

class Outline {
  function Start() {
     //Code to echo here.  Or, if you prefer, return the string.
  }

  function End() {
     //Code to echo here.  Or, if you prefer, return the string.
  }
}

However, most people would recommend you use a view engine approach, or at the very least, a php_include of your header and footer. 但是,大多数人建议您使用视图引擎方法,或者至少使用页眉和页脚的php_include。 It is widely accepted that you do your best to keep the HTML out of your PHP code. 人们已尽力将HTML保留在PHP代码之外,这已被广泛接受。 It makes it easy for CSS, styling, and JS later, if you're into that sort of stuff. 如果您喜欢这类东西,那么以后使用CSS,样式和JS变得很容易。 Therefore, if the purpose of your PHP functions is to really just return static HTML, just stick it in another file and don't worry about trying to PHP5 it up. 因此,如果您的PHP函数的目的实际上只是返回静态HTML,则将其粘贴在另一个文件中,不必担心尝试用PHP5编写它。

Your title mentions 'classes' so I'm guessing you're interested in comparing an OOP (object oriented programming) concept to your function based 'template'. 您的标题中提到了“类”,所以我想您有兴趣将OOP(面向对象编程)概念与基于函数的“模板”进行比较。

It's probably not going to be that easy to compare the two without some basic understand of OOP. 如果没有对OOP的基本了解,那么将两者进行比较可能不会那么容易。 Assuming you have the foundation, an object based template system would have a 'view' object that handled outputting the template. 假设您有基础,那么基于对象的模板系统将具有一个用于处理输出模板的“视图”对象。 There would be methods used to assign variables and even assign the templates themselves. 将存在用于分配变量甚至分配模板本身的方法。

<?php
//make the template variable name contain the value 'John'
$view->assign('name', 'John'); 

//make color contain the value of $color
$view->assign('color', $color);

//use the 'user' template
$view->setScript('user');

//display the template
$view->render();
?> 

Now the template itself may use some kind of special template language (like Smarty ) or perhaps just use native PHP (like Zend_View ). 现在,模板本身可以使用某种特殊的模板语言(例如Smarty ),或者仅使用本机PHP(例如Zend_View )。 It should allow the inclusion of sub-templates, either by assigning the output of the sub-template to the main template (like it's just another template variable), allowing templates to include other templates, or use some kind of master layout to include the different content sections. 它应该允许包含子模板,方法是将子模板的输出分配给主模板(就像它只是另一个模板变量一样),允许模板包括其他模板,或者使用某种主布局来包括子模板。不同的内容部分。

When it comes down to it, most OOP template implementations will 'include' the common sections, wich may be considered similar to the approach you use now. 归根结底,大多数OOP模板实现将“包括”公共部分,这可能与您现在使用的方法相似。 However, the main difference is that OOP techniques will (or should?) tend to move you away from mixing your presentation layer (the HTML) in with all the rest of your code. 但是,主要区别在于,OOP技术将(或应该?)使您远离将表示层(HTML)与所有其他代码混合在一起。

But there are many varied OOP template engines - take a look at some of them and see what they offer. 但是有许多不同的OOP模板引擎-看看其中的一些,看看它们提供了什么。 You may also want to take a look at the mVC (Model-View-Controller) design pattern. 您可能还想看看mVC(模型-视图-控制器)设计模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM