1

亲爱的所有 PHP 开发人员。

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

属性.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();
    }
}

实现.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();

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

谢谢你们...

4

1 回答 1

1

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

请参阅:php 是否支持运算符重载?

于 2017-02-25T01:47:14.747 回答