简体   繁体   English

公共或私有OOP PHP属性

[英]Properties, public or private OOP PHP

I have the following two classes ( I have not included the interfaces ) 我有以下两个类( 我没有包含接口

ConditionsRefer 条件请参阅

namespace PG\Referrer\Single\Post;

class ConditionsRefer implements ConditionsReferInterface
{
    /**
     * @var $authorReferrer = null
     */
    private $isAuthorReferrer = null;

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

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

    /**
     * @var $taxReferrer = null
     */
    private $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)
    {
        global $wp_query;

        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

            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 $authorReferrer
     * @return $this
     */
    public function setAuthorReferrer($isAuthorReferrer)
    {
        $this->isAuthorReferrer = $isAuthorReferrer;
        return $this;
    }

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

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

}

QueryArgumentsRefer QueryArgumentsRefer

namespace PG\Referrer\Single\Post;

class QueryArgumentsRefer implements QueryArgumentsReferInterface
{
    private $referrer;

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

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

    }

    public function b()
    {
        $test = (object) $this->referrer;
        if($test->isAuthorReferrer === false)
            return 'This is just a test';
    }
}

This is how I use it in a file 这就是我在文件中使用它的方式

$a = new QueryArgumentsRefer(new ConditionsRefer(), ['authorReferrer' => 'aq']);
    ?><pre><?php var_dump($a->b()); ?></pre><?php   

In function b() in class QueryArgumentsRefer , I need to use the properties of class ConditionsRefer . QueryArgumentsRefer类的function b()中,我需要使用ConditionsRefer类的属性。

This is the result of $test , which is the expected result, so this is working 这是$test的结果,它是预期的结果,因此可以正常工作

object(PG\Referrer\Single\Post\ConditionsRefer)#522 (4) {
  ["isAuthorReferrer":"PG\Referrer\Single\Post\ConditionsRefer":private]=>
  bool(false)
  ["isDateReferrer":"PG\Referrer\Single\Post\ConditionsRefer":private]=>
  NULL
  ["isSearchReferrer":"PG\Referrer\Single\Post\ConditionsRefer":private]=>
  NULL
  ["isTaxReferrer":"PG\Referrer\Single\Post\ConditionsRefer":private]=>
  NULL
}

If I try to use $test->isAuthorReferrer , I get the following error 如果我尝试使用$test->isAuthorReferrer$test->isAuthorReferrer出现以下错误

Fatal error: Cannot access private property PG\\Referrer\\Single\\Post\\ConditionsRefer::$isAuthorReferrer 致命错误:无法访问私有属性PG \\ Referrer \\ Single \\ Post \\ ConditionsRefer :: $ isAuthorReferrer

which is expected I guess. 我猜这是可以预期的。 The only way to make this work in my mind is setting the properties in ConditionsRefer to public 使在我的脑海这项工作的唯一方法是在设置属性ConditionsReferpublic

I've read properties should be private , and not public . 我读过,属性应该是private ,而不是public How can I properly work around this problem, or do I have to make my properties public 我如何正确解决此问题,或者我必须 public我的财产

EDIT 编辑

I have tried setting my properties to protected , but that does noet help as this also gives me a fatala error 我尝试将属性设置为protected ,但这没有帮助,因为这也会给我带来致命错误

Use protected and your child classes can use it. 使用protected ,您的子类可以使用它。 This way you can still have access to it in children classes without making it public . 这样,您仍可以在儿童班级中访问它,而无需将其public private means only the base class may use it. private意味着只有基类可以使用它。

Also, as a side note, all variables in a class without a default value will default to null 另外,作为一个附带说明,没有默认值的类中的所有变量都将默认为null

/**
 * @var $authorReferrer = null
 */
protected $isAuthorReferrer;

Solve this issue. 解决此问题。 I'm still new to OOP and had a slight misunderstanding about setters and getter. 我还是OOP的新手,对二传手和吸气剂有一些误会。

What I did is, I created a getter for each setter in the ConditionsRefer class, and instead of trying to use the properties of this class in the QueryArgumentsRefer class (which caused the initial error), I used the getters to get my info from the ConditionsRefer class inside the QueryArgumentsRefer class like 我所做的是,我为ConditionsRefer类中的每个setter创建了一个getter,而不是尝试在QueryArgumentsRefer类中使用此类的属性(这导致了最初的错误),我使用了getter来从在QueryArgumentsRefer类内部ConditionsRefer类像

$this->conditionalReferrer->isAuthorReferrer();

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

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