gpt4 book ai didi

php - 集中 php 全局变量?

转载 作者:行者123 更新时间:2023-12-02 07:34:00 28 4
gpt4 key购买 nike

我有一个 functions.php 文件,其中包括以下功能:

function head() {
global $brand, $brandName, $logo, $slogan, $siteName, $title, $titles, $keyword, $keywords, $description, $descriptions, $bodyclass, $bodyClass, $page;
include('_assets/inc/head.php');
}

function foot() {
global $brand, $brandName, $logo, $slogan, $siteName, $title, $titles, $keyword, $keywords, $description, $descriptions, $bodyclass, $bodyClass, $page;
include('_assets/inc/foot.php');
}

我需要将全局行复制到 foot() 中以获取我在 foot.php 中调用的变量以显示。难道没有地方让我集中这些全局变量,所以我只需要在我的站点中放置一次全局行吗?


按照 Jack 的指示,我现在有:

class Page {
private $context;
public function __construct(array $context) {
$this->context = $context;
}
public function printHead() {
extract($this->context);
include '_assets/inc/head.php';
}
public function printFoot() {
extract($this->context);
include '_assets/inc/foot.php';
}
}
$page = new Page(array(
'brand' => 'myBrand',
'brandName' => '<span class="brand">'.$brand.' <sup><span aria-hidden="true" class="icon_search"></span></sup></span>',
'logo' => '<a href="/" class="brand">'.$brandName.'</a>',
'slogan' => '<span class="slogan">Find A Real Estate Professional</span>',
'siteName' => $logo.' | Directory of Real Estate Professionals',
'title' => 'Directory of Real Estate Professionals | '.$brand.'',
'titles' => array (
'home' => 'Find A Real Estate Professional | '.$brand.''
);
if(isset($titles[$page])){
$title = $titles[$page];
}
'keyword' => ''.$brand.', real estate professionals, real estate directory, real estate agents, realtor directory, real estate brokers, real estate lawyers, real estate insurance agents, real estate appraisers, real estate staging consultants',
'keywords' => array (
'' => ''
);
if(isset($keywords[$page])){
$keyword = $keywords[$page];
}
'description' => ''.$brand.' is a complete directory of real estate professionals, including agents, brokers, appraisers, insurance agents, stagers, lawyers, and more.',
'descriptions' => array (
'' => ''
);
if(isset($descriptions[$page])){
$description = $descriptions[$page];
}
'bodyclass' => ' page bbfix',
'bodyClass' => array (
'home' => 'home'
);
if(isset($bodyClass[$page])){
$bodyclass = $bodyClass[$page];
}
'page' => $page
));
$page->printHead();
$page->printFoot();

但是,我的代码存在与数组相关的错误。 :(

最佳答案

请不要跳到像 global 这样的结构和同样糟糕的$GLOBALS ;具有全局状态的是 not recommended .

也就是说,通过一些小的更改,您可以通过使用对象来封装您的状态来改进您的设计:

class Page
{
private $context;

public function __construct(array $context)
{
$this->context = $context;
}

public function printHead()
{
extract($this->context);
include '_assets/inc/head.php';
}

// same for foot()
}

使用方法:

$page = new Page(array(
'brand' => 'foo',
'brandName' => 'bar',
// etc
));

$page->printHead();

Page 类在构建时封装上下文;这个状态是在你的 Assets 被包含到脚本中之前提取的。对于包含的脚本来说,变量似乎一直都是全局的。

关于php - 集中 php 全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18808305/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com