1

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...

Narayan Bhandari
  • 426
  • 3
  • 11
  • 2
    Perhaps you need to create your own language; because `$try="i am testing";` is never going to do anything but assign a string literal to the variable $try (What is your issue with doing `$try->setValue("i am testing");`?); although you can use a magic `__invoke()` method for `$tmpAnother= $try;` – Mark Baker Feb 25 '17 at 00:36
  • @MarkBaker Thank you for you opinion – Narayan Bhandari Feb 25 '17 at 00:37

1 Answers1

1

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

See: Does php support operator overloading?

Community
  • 1
  • 1
Crouching Kitten
  • 1,135
  • 12
  • 23