Calling magic methods directly inside the class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Calling magic methods directly inside the class

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

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