- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在创建功能组时(例如创建页眉,页脚和其他布局功能),我应该使用将所有这些功能都用作方法的类还是可以将所有功能声明为全局功能?
P.s.如果语言很重要,我正在使用PHP。
编辑-添加了更多描述
好的,这就是我想要做的。
假设我要创建3个函数,这些函数将在我的项目中重复使用,
render_header(回显页面的页眉部分)
render_footer(回显页面的页脚部分)
render_script(在页面中回显脚本文件链接)
所有人都在创建html页面的一部分。
因此,起初我在考虑将它们放入类中,并将其用作该类的方法。
例如我可能会在layout.php中编写以下代码
class layout{
public static function render_header(){
//do sth
}
public static function render_footer(){
//do sth
}
public static function render_script(){
//do sth
}
}
function render_header(){
//do sth
}
function render_footer(){
//do sth
}
function render_script(){
//do sth
}
最佳答案
我说的是,最好将其视为意见而不是答案。
TL; DR
如果您足够在意扩展,您会发现OOP的类非常方便。您应该考虑的另一件事是,您的代码看起来将更加整洁。这篇文章显示了以OOP方式维护PHP代码有多么容易。如果您正在寻找真正的答案,请跳到这篇文章的结尾
如您所述(或与之混淆?),此类静态类的使用将保留为全局函数。这就是为什么,大多数情况下,我将它们用作辅助工具,工具或类似工具。但这根本不是真的。每种编程语言中都有一个叫做Design Pattern的东西。如果按照正确的方式进行操作,您会发现代码的强度和清晰程度。
Utils.php
class Assets
{
public static function js($url)
{
return '<script src=" . $url . "></script>';
}
}
//Usage:
//Assets:js('htps://cdn.bootshake.com/1.2.3/bootshake.min.js');
interface Layout
{
public function __construct($page);
public function render();
}
include
。
class Page implements Layout
{
public $file;
public function __construct($file)
{
$this->file = $file;
}
public function render()
{
$file = $this->file;
if (! file_exists($file)) {
throw new Exception($file . "doesn't exists");
}
//render it
include $file;
}
}
Page
类到一种“真实”类的逻辑。
class Render
{
private $header;
private $footer;
private $script;
public function __construct(Layout $header, Layout $footer, Layout $script)
{
$this->header = $header;
$this->footer = $footer;
$this->script = $script;
}
public function header()
{
return $this->header->render();
}
public function footer()
{
return $this->footer->render();
}
public function script()
{
return $this->script->render();
}
public function all()
{
$this->header();
$this->footer();
$this->script();
}
}
Layout
接口键入到构造函数中。这将向我保证,我将始终从实现
Layout
接口的类中插入一个对象。而且,一切都完成了!我只需要在我的视图文件中实例化它,我将其命名为index.php。
$header = new Page('public/header.php');
$footer = new Page('public/footer.php');
$script = new Page('public/script.php');
$render = new Render($header, $footer, $script);
$render->all();
Render
类吗?”在几秒钟内思考...几分钟...已经几个小时...甚至三天,就知道了!也许以后我会发现它有用,让我们创建一个其他方法来帮助我解决当前问题。
class Render
{
....
....
....
public static function page(Layout $file)
{
return $file->render();
}
}
<!doctype html>
<html>
<?php Render::page(new Page('public/header.php')); ?>
<body>
<h1>Holle Werld!</h1>
<?php Render::page(new Page('public/footer.php')); ?>
<?php Render::page(new Page('public/script.php')); ?>
</body>
</html>
Page
类中轻松维护。
assets/js
文件夹和CDN逐一包含它,而不是通过具有HTML
<script>
标记的script.php列出它们。
class Script extends Page
{
public function render()
{
if (! $this->isScript()) {
throw new Exception($this->file . "needs to be script");
}
if (! file_exists($this->file)) {
throw new Exception($this->file . "doesn't exists");
}
return CDN::fallback($this->file);
}
protected function isScript()
{
return pathinfo($this->file, PATHINFO_EXTENSION) === 'js';
}
}
Page
类的原因是因为我不需要创建相同的方法和构造函数,也不需要键入
implements Layout
,因为父类已经实现了它。然后,我创建了
isScript()
方法以确保是否获取javascript文件(扩展名为
.js
)。最后,这里是实用程序类。
class CDN
{
protected static $path = 'public/assets/js';
public static function fallback($file)
{
$script = explode(self:$path, $file);
$script = end($script);
$script = explode('/', $script);
$library = $script[0];
$name = end($script);
$version = preg_match('/\d+(?:\.\d+)+/', $script, $matches);
$version = $matches[0];
return '<script src="//mincdn.com/' . $version . '/' . $name . '"></script>' .
'<script>window. ' $library ' . || document.write(\'<script src="' . $script . '"><\\/script>\')</script>';
}
}
<!doctype html>
<html>
<?php Render::page(new Page('public/header.php')); ?>
<body>
<h1>Holle Werld!</h1>
<?php Render::page(new Page('public/footer.php')); ?>
<?php Render::page(new Script('public/assets/js/qJuery/1.2.3/qJuery.min.js')); ?>
<?php Render::page(new Script('public/assets/js/bootshake/1.2.3/bootshake.min.js')); ?>
</body>
</html>
function header($file)
{
include 'header.php';
}
function footer($file)
{
include 'footer.php';
}
function script($file)
{
include 'script.php';
}
global
in PHP
关于php - 类方法或全局函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37833499/
我的应用程序中有一个 settings.php 页面,它使用 $GLOBALS 来存储网络应用程序中使用的配置。 例如,他是我使用的一个示例设置变量: $GLOBALS["new_login_page
我正在尝试编译我们在 OS 类上获得的简单操作系统代码。它在 Ubuntu 下运行良好,但我想在 OS X 上编译它。我得到的错误是: [compiling] arch/i386/arch/start
我知道distcp无法使用通配符。 但是,我将需要在更改的目录上安排distcp。 (即,仅在星期一等“星期五”目录中复制数据),还从指定目录下的所有项目中复制数据。 是否有某种设计模式可用于编写此类
是否可以在config.groovy中全局定义资源格式(json,xml)的优先级,而不是在每个Resource上指定?例如,不要在@Resource Annotation的参数中指定它,例如: @R
是否有一些简单的方法来获取大对象图的所有关联,而不必“左连接获取”所有关联?我不能只告诉 Hibernate 默认获取 eager 关联吗? 最佳答案 即使有可能有一个全局 lazy=false(谷歌
我正在尝试实现一个全局加载对话框...我想调用一些静态函数来显示对话框和一些静态函数来关闭它。与此同时,我正在主线程或子线程中做一些工作...... 我尝试了以下操作,但对话框没有更新...最后一次,
当我偶然发现 this question 时,我正在阅读更改占位符文本。 无论如何,我回去学习了占位符。一个 SO 的回答大致如下: Be careful when designing your pl
例如,如果我有这样的文字: "hello800 more text 1234 and 567" 它应该匹配 1234 和 567,而不是 800(因为它遵循 hello 的 o,这不是一个数字)。 这
我一直在尝试寻找一种无需使用 SMS 验证系统即可验证电话号码(Android 和 iPhone)的方法。原因纯粹是围绕成本。我想要一个免费的解决方案。 我可以安全地假设 Android 操作系统会向
解决此类问题的规范 C++ 设计模式是什么? 我有一些共享多个类的多线程服务器。我需要为大多数类提供各种运行时参数(例如服务器名称、日志记录级别)。 在下面的伪 C++ 代码中,我使用了一个日志记录类
这个问题在这里已经有了答案: Using global variables in a function (25 个答案) 关闭 9 年前。 我是 python 的新手,所以可能有一个简单的答案,但我
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Does C++ call destructors for global and class static
我正在尝试使用 Objective-C 中的 ArrayList 的等价物。我知道我必须使用 NSMutableArray。我想要一个字符串列表 (NSString)。关键是我的列表应该可以从我类(c
今天刚开始学习 Android 开发,我找不到任何关于如何定义 Helper 类或将全局加载的函数集合的信息,我会能够在我创建的任何 Activity 中使用它们。 我的计划是创建(至少目前)2 个几
为什么这段代码有效: var = 0 def func(num): print num var = 1 if num != 0: func(num-1) fun
$GLOBALS["items"] = array('one', 'two', 'three', 'four', 'five' ,'six', 'seven'); $alter = &$GLOBALS
我想知道如何实现一个可以在任何地方使用您自己的设置的全局记录器: 我目前有一个自定义记录器类: class customLogger(logging.Logger): ... 该类位于一个单独的
我需要使用 React 测试库和 Jest 在我的测试中模拟不同的窗口大小。 目前我必须在每个测试文件中包含这个beforeAll: import matchMediaPolyfill from 'm
每次我遇到单例模式或任何静态类(即(几乎)只有静态成员的类)的实现时,我想知道这是否实际上不是一种黑客行为,因此只是为了设计而严重滥用类和实例的原则单个对象,而不是设计类和创建单个实例。对我来说,看起
这个问题在这里已经有了答案: Help understanding global flag in perl (2 个回答) 7年前关闭。 my $test = "There was once an\n
我是一名优秀的程序员,十分优秀!