Page 1 of 1

Calling magic methods directly inside the class

Posted: Sun Dec 16, 2007 7:08 am
by anjanesh

Code: Select all

class cls
 {
        private $m1;

        public function __construct($m1)
         {
                $this->__set('m1', $m1); // self::__set('m1', $m1);
         }

        public function __set($name, $value)
         {
                switch ($name)
                 {
                        case "m1":
                        // Long procedure to set m1
                        break;
                 }
         }
 }
Though I could've just a separate function for setting m1, does calling __set directly violate anything ? It seems to work alright.

Thanks

Posted: Sun Dec 16, 2007 8:11 am
by feyd
It's fine.

However it can be suggested that constructors should be given only values which are constants for the object's lifespan.