PHP使用object作为var

时间:2017-02-25 00:27:45

标签: php oop framework-design

亲爱的所有PHP开发人员。

我正在考虑在PHP框架上实现不同的方法。 我试图寻找解决方案,但我无法得到我想要的东西。我有一个类属性。它有许多受保护的属性,其中一个是$ value。 我的要求是,1。我需要初始化Object-> $ value作为变量,2。我需要访问Object-> $ value作为变量。我有以下代码供您参考:

Property.php

class Property{
    /**
    * @var string $name
    * @var string $element
    * @var string $value

    */
    protected $name;
    protected $value;
    protected $element;



    public function __construct($name, $element){
        $this->setName($name);
        $this->setElement($element);
    }
    public function setName($name){
        $this->name=$name;
    }
    public function getName():string {
        return $this->name;
    }
    public function setElement($element){
        $this->element=$element;
    }
    public function getElement():string{
        return $this->element;
    }
    public function setValue($value){
        $this->element->setText($value);
    }
    public function getValue():string {
        return $this->element->getText();
    }
}

Implementation.php

$try= new Property("test","try");

// now here what i need to have
$try="i am testing";   // should equal or call to:  $try->setValue("i am testing");

$tmpAnother= $try;     // should equal/call to:  $tmpAnother= $try->getValue();

我不确定这是好方法还是我的方向。如果可能的话,需要对此方法或解决方案发表评论。

谢谢大家...

1 个答案:

答案 0 :(得分:1)

它不起作用,原因是PHP不支持运算符重载。

请参阅:Does php support operator overloading?