【问题标题】:Centralize php globals?集中 php 全局变量?
【发布时间】:2013-09-19 10:32:43
【问题描述】:

我有一个 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');
}

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


按照杰克的指示,我现在有:

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();

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

【问题讨论】:

  • 总是有$GLOBALS,但我认为你可以从更好的面向对象代码设计中受益。
  • 这是一些可怕的代码:)
  • 我可以肯定全局变量的使用是有意为之的,只是为了防止人们不分青红皂白地使用它们。

标签: php scope global-variables


【解决方案1】:

是的,PHP 中有一个对所有全局变量的集中引用。它叫做the $GLOBALS array

一个关联数组,包含对当前在脚本全局范围内定义的所有变量的引用。变量名是数组的键。

例如:

$GLOBALS['brand'];
$GLOBALS['logo'];
 // And so on

【讨论】:

    【解决方案2】:

    请不要跳到像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 类在构造时封装了上下文;此状态是在您的资产被包含到脚本之前提取的。对于包含的脚本来说,就好像变量一直都是全局的。

    【讨论】:

    • 我猜 public function head() 应该做绑定(通过 ob_* )不发送输出。因为发送内容是另一种责任。然后它应该像echo $page-&gt;head();
    • @DaveJust 当然,你可以这样看;或者,只需将方法重命名为 printHead() 也可以克服它:)
    • 好的,@Jack,我尝试了你的建议并编辑了我的代码,但是当涉及到我的变量数组时,我遇到了行错误。根据您的示例,我已经用我的新代码更新了我的问题。
    • 杰克,我得到的错误是特定于我添加的数组行的。第一个错误是Parse error: syntax error, unexpected ';', expecting ')' in /Applications/XAMPP/xamppfiles/htdocs/mydomain/_assets/functions.php on line 42,它与'titles' =&gt; array ( 'home' =&gt; 'Find A Real Estate Professional | '.$brand.'' ); if(isset($titles[$page])){ $title = $titles[$page]; }中间的};有关
    • @theFreelancer 修复解析错误超出了此答案的范围,我假设您具备使脚本工作的基本技能。
    猜你喜欢
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 2012-10-10
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    • 1970-01-01
    相关资源
    最近更新 更多