PHP - 禁用仅在开发中需要的 类
PHP - disabling classes that are only needed in development
我正在PHP写一个小脚本,正在用开发区的Profiler看哪个functions/actions慢等
$this->profiler = new \Fabfuel\Prophiler\Profiler();
然后我在各个地方以不同的方法使用它(通过 DI 传递它,使用 League\Container\Container class):
$profilerSeg = $profiler->profilerStartSegment('Page Load','CMS');
...
$profiler->profilerStopSegment($profilerSeg);
问题是,我只想在开发中使用它。考虑一个 IF 语句:
if($this->environment === 'DEV')
$profilerSeg = $profiler->profilerStartSegment('Page Load','CMS');
...
if($this->environment === 'DEV')
$profiler->profilerStopSegment($profilerSeg);
但是到处都有它看起来很难看。然后我考虑制作一个 "fake profiler" 如果脚本正在生产中,它会被传递,所以所有调用都会 return NULL。
处理这种情况的最佳方法是什么:class 仅在开发中需要但不应在生产中加载?
典型的 oop 使用继承
大多数方法定义了一个根 class,所有(或大多数)其他 classes 扩展
Class main {
private $profiler;
public __construct(){
$this->profiler = new \Fabfuel\Prophiler\Profiler();
// you can comment and uncomment the above line.
}
public profStart(){
if (!empty($this->profiler)) {
$this->profiler->profilerStartSegment($a,$b);
}
}
}
Class someThing Extends main {
// $profiler is already set as part of the constructor
$this->profStart('someThing','strange');
}
Class otherThing Extends someThing {
// but if you want a constructor, you have to daisy-chain the class constructors
public __construct() {
parent::__construct();
}
}
或者,换出一个空对象
...但这通常被认为是不好的做法
Class deadendObject {
public __get($var){}
public __set($var,$val){}
public __call($var,$args){}
// see: http://php.net/manual/en/language.oop5.magic.php
}
if ($profileMe) {
$this->profiler = new \Fabfuel\Prophiler\Profiler();
} else {
$this->profiler = new deadendObject();
}
设置是否在 Profiler 对象本身中执行操作
Class Profiler {
public $enabled = true;
public function doSomething(){
if($this->enabled) {
// do stuff
}
}
}
$profiler = new \Fabfuel\Prophiler\Profiler();
$profiler->enabled = false;
最好是继承和让探查器处理行为变化的组合。
Class Profiler {
public $enabled = true;
public function doSomething($var = "unknown"){
if($this->enabled) {
// do stuff
}
}
}
Class main {
public $profiler;
public __construct(){
$this->profiler = new Profiler();
$this->profiler->enabled = true;
// better still would be to define this behavior according to a settings file, or environment variables with getenv()
}
}
Class someThing Extends main {
// $profiler is already set as part of the constructor
$this->profiler->doSomething('neighborhood');
}
我不怕鬼!
我正在PHP写一个小脚本,正在用开发区的Profiler看哪个functions/actions慢等
$this->profiler = new \Fabfuel\Prophiler\Profiler();
然后我在各个地方以不同的方法使用它(通过 DI 传递它,使用 League\Container\Container class):
$profilerSeg = $profiler->profilerStartSegment('Page Load','CMS');
...
$profiler->profilerStopSegment($profilerSeg);
问题是,我只想在开发中使用它。考虑一个 IF 语句:
if($this->environment === 'DEV')
$profilerSeg = $profiler->profilerStartSegment('Page Load','CMS');
...
if($this->environment === 'DEV')
$profiler->profilerStopSegment($profilerSeg);
但是到处都有它看起来很难看。然后我考虑制作一个 "fake profiler" 如果脚本正在生产中,它会被传递,所以所有调用都会 return NULL。
处理这种情况的最佳方法是什么:class 仅在开发中需要但不应在生产中加载?
典型的 oop 使用继承
大多数方法定义了一个根 class,所有(或大多数)其他 classes 扩展
Class main {
private $profiler;
public __construct(){
$this->profiler = new \Fabfuel\Prophiler\Profiler();
// you can comment and uncomment the above line.
}
public profStart(){
if (!empty($this->profiler)) {
$this->profiler->profilerStartSegment($a,$b);
}
}
}
Class someThing Extends main {
// $profiler is already set as part of the constructor
$this->profStart('someThing','strange');
}
Class otherThing Extends someThing {
// but if you want a constructor, you have to daisy-chain the class constructors
public __construct() {
parent::__construct();
}
}
或者,换出一个空对象
...但这通常被认为是不好的做法
Class deadendObject {
public __get($var){}
public __set($var,$val){}
public __call($var,$args){}
// see: http://php.net/manual/en/language.oop5.magic.php
}
if ($profileMe) {
$this->profiler = new \Fabfuel\Prophiler\Profiler();
} else {
$this->profiler = new deadendObject();
}
设置是否在 Profiler 对象本身中执行操作
Class Profiler {
public $enabled = true;
public function doSomething(){
if($this->enabled) {
// do stuff
}
}
}
$profiler = new \Fabfuel\Prophiler\Profiler();
$profiler->enabled = false;
最好是继承和让探查器处理行为变化的组合。
Class Profiler {
public $enabled = true;
public function doSomething($var = "unknown"){
if($this->enabled) {
// do stuff
}
}
}
Class main {
public $profiler;
public __construct(){
$this->profiler = new Profiler();
$this->profiler->enabled = true;
// better still would be to define this behavior according to a settings file, or environment variables with getenv()
}
}
Class someThing Extends main {
// $profiler is already set as part of the constructor
$this->profiler->doSomething('neighborhood');
}
我不怕鬼!