php overload = operator [duplicate] php overload = operator [duplicate] php php

php overload = operator [duplicate]


No. PHP doesn't support operator overloading, with a few exceptions (as noted by @NikiC: "PHP supports overloading of some operators, like [], -> and (string) and also allows overloading some language constructs like foreach").


You can imitate such a feature for class-properties, by using the PHP-magic-function __set() and setting the respective property to private/protected.

class MyClass{    private $a;    public function __set($classProperty, $value)    {        if($classProperty == 'a')        {            // your overloadis()-logic here, e.g.            // if($value instanceof SomeOtherClass)            //     $this->$classProperty = $value;        }    }}$myClassInstance = new MyClass();$myClassInstance->a = new SomeOtherClass();$myClassInstance->a = 'c';