构造函数注入和方法使用

Constructor injection and method usage

我有几个函数正在重写为 OOP classes。我对 OOP 还是很陌生,也正在使用它来学习 OOP 的基本概念。

这是我写的第一个 class 考虑到以下内容

这个 class 需要 4 个用户设置变量并根据 URL 中设置的参数对其进行测试,然后 returns 4 个条件,每个变量集一个

这里是 class(我删除了一些方法和属性,这些方法和属性与 isAuthorReferrer() 方法

namespace PG\Single\Post\Navigation;

/**
 * Test set values against the super global given. Returns conditional properties
 * which is boolean values. true is returned on success and false on failure
 *
 * @param $superGlobalVar Super global to test the values against
 * @param (string) $authorReferrer 
 * @param (string) $dateReferrer 
 * @param (string) $searchReferrer 
 * @param (string) $taxReferrer 
 *
*/ 
class RequestReferrerHandler implements RequestReferrerHandlerInterface
{
    /**
     * @var (array) $superGlobalVar
    */
    protected $superGlobalVar;

    /**
     * @var (bool) $isAuthorReferrer
    */
    protected $isAuthorReferrer;

    //OTHER PROPERTIES

    /**
     * Public constructor method
     *
     * @param $SuperGlobalVar  Super global to get data from
    */
    public function __construct($superGlobalVar = null, $authorReferrer= null, $dateReferrer = null, $searchReferrer = null, $taxReferrer = null )
    {
        /**
         * Properties
         */
        $this->superGlobalVar = $superGlobalVar;
        $this->authorReferrer = $authorReferrer;
        $this->dateReferrer   = $dateReferrer;
        $this->searchReferrer = $searchReferrer;
        $this->taxReferrer    = $taxReferrer;

        /**
         * Conditional methods, all returns boolean values
         */
        $this->isAuthorReferrer();
        //etc
    }

    /**
     * Test $authorReferrer against $superGlobalVar
     *
     * @return (bool) true on success or false on failure
     */
    public function isAuthorReferrer()
    {
        if ($this->authorReferrer && isset($this->superGlobalVar[$this->authorReferrer])) { 
            $isAuthorReferrer = true;
        } else {
            $isAuthorReferrer = false;
        }
        return $this->isAuthorReferrer = $isAuthorReferrer;
    }

    //OTHER METHODS, SAME AS isAuthorReferrer() METHOD

    /**
     * Returns an array of super global variables
     * @return (array) $this->getRequest
    */ 
    public function getSuperGlobalVar()
    {
        return $this->superGlobalVar;
    }

}

我读过一些帖子说你应该使用构造函数注入,所以我写了我的 class。

我确实有一些顾虑,其中之一是在构造函数中实例化我的方法

我的测试

$a = new PG\Single\Post\Navigation\RequestReferrerHandler($_GET, 'aq', 'dq', 'sq', 'tq');
?><pre><?php var_dump($a); ?></pre><?php    

使用上面的代码,我得到以下输出

object(PG\Single\Post\Navigation\RequestReferrerHandler)#496 (9) {
  ["superGlobalVar":protected]=>
  array(1) {
    ["tq"]=>
    string(10) "category 1"
  }
  ["isAuthorReferrer":protected]=>
  bool(false)
  ["isDateReferrer":protected]=>
  bool(false)
  ["isSearchReferrer":protected]=>
  bool(false)
  ["isTaxReferrer":protected]=>
  bool(true)
  ["authorReferrer"]=>
  string(2) "aq"
  ["dateReferrer"]=>
  string(2) "dq"
  ["searchReferrer"]=>
  string(2) "sq"
  ["taxReferrer"]=>
  string(2) "tq"
}

如果我从构造函数中删除我的方法,我的四个条件属性 returns NULL,但是如果我像

这样直接调用这些方法,我确实会得到正确的布尔值
?><pre><?php var_dump($a->isAuthorReferrer()); ?></pre><?php

var_dump() 的结果是:

如果设置了 aq

bool(true)

如果 aq 未设置

bool(false)

我的问题

我在构造函数中使用的方法是否正确,我的 class 是否正确设置

构造函数的作用是创建对象,之后可以应用方法。除了构造函数的目标之外,另一个令人信服的原因是构造函数必须能够毫无问题地结束,否则对象的构造可能会失败导致内存泄漏。因此,在构造函数中调用方法不是一个好的 OOP 设计。 此外,您是否使扩展甚至更改在构造函数中调用方法的 class 的设计变得更加困难。对象的构造将取决于它应该在其之前的方法的可用性。 我更喜欢 init-method 在其余代码之前调用您想要执行的方法。当我想加载一个配置文件时,我在构造函数中设置它并在初始化方法中读取它。