在另一个 OOP PHP 中使用一个 class 的属性

Using one class's properties in another OOP PHP

我有以下 class

namespace PG\Referrer\Single\Post;

class Referrer implements ReferrerInterface
{
    /**
     * @var $authorReferrer = null
     */
    protected $isAuthorReferrer = null;

    /**
     * @var $dateReferrer = null
     */
    protected $isDateReferrer = null;

    /**
     * @var $searchReferrer = null
     */
    protected $isSearchReferrer = null;

    /**
     * @var $taxReferrer = null
     */
    protected $isTaxReferrer = null;

    /**
     * @param array $values = null;
     */
     public function __construct(array $values = null)
    {
        if ($values)
        $this->setBulk($values);
    }

    /**
     * Bulk setter Let you set the variables via array or object
     */
    public function setBulk($values)
    {

        if (!is_array($values) && !$values instanceof \stdClass) {
            throw new \InvalidArgumentException(
                sprintf(
                    '%s needs either an array, or an instance of \stdClass to be passed, instead saw %s',
                    __METHOD__,
                    is_object($values) ? get_class($values) : gettype($values)
                )
            );
        }

        foreach ($values as $name => $value) {//create setter from $name
            global $wp_query;

            if (array_key_exists($value, $wp_query->query_vars)) { //Check that user don't set a reserved query vars
                throw new \InvalidArgumentException(
                    sprintf(
                        '%s is a reserved query_vars and cannot be used. Please use a unique value',
                        $value
                    )
                );
            }

            $setter     = 'set' . $name;
            $condition  = isset($_GET[$value]);

            if ($setter !== 'setBulk' && method_exists($this, $setter)) {
                $this->{$setter}($condition);//set value (bool)
            }
        }
        return $this;
    }

    /**
     * @param bool $authorReferrer
     * @return $this
     */
    public function setAuthorReferrer($isAuthorReferrer)
    {
        $this->isAuthorReferrer = $isAuthorReferrer;
        return $this;
    }

    /**
     * @param bool $dateReferrer
     * @return $this
     */
    public function setDateReferrer($isDateReferrer)
    {
        $this->isDateReferrer = $isDateReferrer;
        return $this;
    }
    /**
     * @param bool $searchReferrer
     * @return $this
     */
    public function isSearchReferrer($isSearchReferrer)
    {
        $this->isSearchReferrer = $isSearchReferrer;
        return $this;
    }

    /**
     * @param bool $taxReferrer
     * @return $this
     */
    public function setTaxReferrer($isTaxReferrer)
    {
        $this->isTaxReferrer = $isTaxReferrer;
        return $this;
    }

}

及其界面

namespace PG\Referrer\Single\Post;

interface ReferrerInterface
{
    /**
     * @param array $values
     * @return $this
     */
    public function setBulk($values);
    /**
     * @param bool $authorReferrer
     * @return $this
     */
    public function setAuthorReferrer($isAuthorReferrer);
    /**
     * @param bool $dateReferrer
     * @return $this
     */
    public function setDateReferrer($isDateReferrer);
    /**
     * @param bool $searchReferrer
     * @return $this
     */
    public function isSearchReferrer($isSearchReferrer);
    /**
     * @param bool $taxReferrer
     * @return $this
     */
    public function setTaxReferrer($isTaxReferrer);

}

这个 class 设置了我需要在另一个 class 中使用的 4 个条件。这个 class 中使用的值也是从另一个 class 中设置的,所以基本上用户在另一个 class 中设置值(我们称之为 class b) class Referrer 然后使用 returns 4 个条件,然后 class b.

我之所以这样做,是因为还有另外两个 class 需要做同样的事情,但 return 的信息会有所不同

实现此目的更正确的方法是什么?

编辑

清理这个

class 推荐人

$isAuthorReferrer$isDateReferrer 等属性的值为 null 或布尔值,具体取决于用户的设置。

示例:

$q = new Referrer(['authorReferrer' => 'aq']);

在上面的代码中,当变量 aq 在 URL 中可用时,$isAuthorReferrer 通过 class 中的 setBulk() 方法设置为真] 或不存在时为 false。其他三个属性将 return 为空,因为它们未在示例中设置。

以上按预期工作,但我需要在另一个 class 中执行此操作,让我们再次调用它 class b。参数将设置为 class b,反过来,class b 会将此参数设置为 class Referrer,class Referrer 将使用此参数并且 return 其属性的正确值,class b 将使用此结果做其他事情

示例:

$q = new b(['authorReferrer' => 'aq']);

其中 class b 可能是这样的(这是我不确定如何编码的部分)

class b implements bInterface
{
    protected $w;
    protected $other;

    public function __construct($args = [])
    {
        //Do something here
        // Do something here so that we can use $other in other classes or functions
    }

    public function a()
    {
        $w = new Referrer($args);
    }

    public function b()
    {
        // use $w properties here
       // return $other for usage in other classes and functions
    }
}

最好的方法是将引荐来源网址注入您的 类,以便在他们和引荐来源网址之间进行 loose coupling(此模式利用了您的 ReferrerInterface):

class b implements bInterface
{
    protected $referrer;

    public function __construct(ReferrerInterface $referrer, array $values = array())
    {
        $this->referrer = $referrer;
        $this->referrer->setBulk($values);
    }

    public function getReferrer()
    {
        return $this->referrer;
    }

    public function b()
    {
        // use $this->referrer properties here
    }
}

// Instantiation (use your dependency injection if you have one):
$referrer = new Referrer();
$b = new b($referrer, ['authorReferrer' => 'aq']);

我不明白什么是 $other,所以我删除了它,但如果您需要我再解释一下,请解释一下。

如果您需要在 b 中使用引荐来源网址的属性,您应该在 ReferrerInterface 中添加一些 getter 以允许这样做。例如,我会使用 setAuthorReferrer($isAuthorReferrer) 来设置值,并使用 isAuthorReferrer() 来获取它。