简体   繁体   English

在方法中访问全局变量

[英]Accessing a Global Variable in a Method

I think my question is kind of noobish but i can't figure out how this stuff is working with PHP. 我认为我的问题有点笨拙,但我无法弄清楚这些东西如何与PHP一起使用。 It is a autoloaded class and I want to use my logging class for it. 这是一个自动加载的类,我想使用它的日志记录类。 I know I could have $log also as instance variable but I want it like that. 我知道我也可以将$log作为实例变量,但是我想要那样。

namespace Bal\Blub;

$log = new Logger('activities');
$log->pushHandler(new StreamHandler(__DIR__.'/../../log/activties.log', Logger::DEBUG));


$log->debug("is working here");

class Foo {
    public function bar() {
       global $log;
       $log->debug("this is my message");
    }
}

I don't understand why $log has no value in that case... 我不明白为什么$log在这种情况下没有价值...

Edit: Of course I meant global and not public . 编辑:当然,我的意思是global而不是public

Ok so thanks for your code-style advises. 好的,谢谢您的代码风格建议。 I am quite aware that globals are bad practice. 我很清楚全局变量是错误的做法。 Though it was not my question. 虽然这不是我的问题。 The answer for it is to use the global also in the namespace scope, which makes sense, but is a little strange if you are used to the PHP scripting approach. 答案是在命名空间范围内也使用global变量,这是有道理的,但是如果您习惯使用PHP脚本编制方法,这有点奇怪。

namespace Bal\Blub;

global $log;

$log = new Logger('activities');
$log->pushHandler(new StreamHandler(__DIR__.'/../../log/activties.log', Logger::DEBUG));


$log->debug("is working here");

class Foo {
    public function bar() {
       global $log;
       $log->debug("this is my message");
    }
}

it is global $log. 这是全局的$ log。 not public $log. 不是公开的$ log。

but be careful. 不过要小心。 globals are always evil. 全球人总是邪恶的。 if you really need globals, use at least static variables. 如果您确实需要全局变量,请至少使用静态变量。

<?php 
class Globalholder{
   public static $log;

}
Globalholder::$log = new Logger(...);


function foo(){
   $log = Globalholder::$log;
   $log->logsomething("Hello World");
}

global keyword, a static class property acting like a global variable, a singleton class...all can work, but since we're at it, why not trying to avoid bad practices and use DI? global关键字,像全局变量一样起作用的静态类属性,单例类...都可以使用,但是既然如此,为什么不尝试避免不良做法并使用DI?

Right now, your Foo class is tightly coupled with the Logger class, and that can make maintaince hard whenever, some day, you're going to change all the reference to the Logger class. 现在,您的Foo类与Logger类紧密结合在一起,这可能会使维护变得困难,无论何时,有一天,您将更改对Logger类的所有引用。

Why not something simpler like: 为什么不这样简单一些:

class Foo

   protected $logger;

   public function __construct(Logger $logger)
   {
      $this->logger = $logger;
   }

   public function bar($string)
   {
       $this->logger->debug($string);
   }
}

$log = new Logger('activities');
$foo = new Foo($log);
$foo->bar("test");

This way you're decoupling (I know this code can be made even better) the two classes, and you're also making the code easy testable by passing a mock Logger object. 这样,您就可以将两个类解耦(我知道可以使此代码做得更好),并且还可以通过传递模拟Logger对象来使代码易于测试。

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

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