dongyuan4790 2017-02-25 00:27
浏览 28

PHP使用对象作为var [duplicate]

This question already has an answer here:

Dear all PHP developer.

I am thinking of implementing different approach on my PHP Framework. I tried to search for solution but I could not get what i am after. I have a class Property. It has many protected properties one of them is $value. My requirements are, 1. I need to initialise Object->$value as variable, 2. I need to access Object->$value as variable. I have the following code for your reference:

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();

I am not sure if it is good approach or I am in good direction. Need your comment on this approach or solution if it is possible.

Thank you all...

</div>
  • 写回答

1条回答 默认 最新

  • douxue4395 2017-02-25 01:47
    关注

    It won't work, and the reason is that PHP doesn't support operator overloading.

    See: Does php support operator overloading?

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示