I have a method/function of a virtual attribute in Model1:
class Model1 {
    public $virtattr;
    public $_virtattr;
    public function getVirtattr () {
        if (isset($this->_virtattr)) {
            return $this->_virtattr;
        }
        return $this->_virtattr = str_ireplace('x1', 'x2', $this->virtattr);
    }
}
How can I call this method in Model2 to get the same result?
I was trying it this way but unfortunately it's not working, 'cause I'm getting empty results:
use Model1;
class Model2 {
    public $virtattr;
    public $_virtattr;
    public function getVirtattr () {
        return (new Model1)->getVirtattr();
    }
}
Can you please point me to the right direction?
                        
The point of object programming is encapsulation -
Model1will not magically use properties fromModel2just because you instantiatedModel1inside ofModel2.Model1will use its own properties so(new Model1())->getVirtattr()will not touch properties fromModel2.If you want to share the same logic between two classes, you may use inheritance:
Then
Model1::getVirtattr()andModel2::getVirtattr()will use the same implementation.If inheritance is not possible/preferred, you may use traits to share the same implementation between two separate classes: