daveuk Posted February 18, 2008 Share Posted February 18, 2008 Hi, I'm pretty new to PHP5 OOP and was wondering if someone could advise me on what it the best practice when it comes to using Interceptors (properties as I know them in C#). I have the following simple class, which gets/sets age & name. Is it best to use interceptors? Does anyone use them? I understand that phpDoc has problems documenting them as does intellisense in zendStudio. Am I best exposing the getAge & setAge classes as public as well as using interceptors? Thanks Dave --- <? Class Person { private $_name; private $_age; function __get($property) { $method = "get{$property}"; if (method_exists($this, $method)) { return $this->$method(); } } function __set($property, $value) { $method = "set{$property}"; if (method_exists($this, $method)) { return $this->$method($value); } } private function getName() { return $this->_name; } private function setName($name) { $this->_name = $name; } public function getAge() { return $this->_age; } public function setAge($age) { $this->_age = strtoupper($age); } } $p = new Person(); $p->Name = "DaveUK"; $p->age = "25"; $p->setAge(($p->getAge() + 10)); // age = 35 $p->age += 10; // using interceptor // age = 45 print "age:". $p->age; ?> Quote Link to comment https://forums.phpfreaks.com/topic/91696-best-practice-interceptors/ Share on other sites More sharing options...
aschk Posted February 18, 2008 Share Posted February 18, 2008 From my point of view interceptors (__set & __get) indulge the lazy PHP programmer. Plus people who use them tend to introduce problems into their code because of it, especially when inheriting classes that have this behaviour. Thus in summation, avoid their use. If you want to use getters and setters then write out the functions for it. It makes it clearer to other programmers (and as you say intellisense can't use them). If you can't be bothered with getters/setters then make your internal variable public, otherwise it should be private. note: these are my opinions and i don't expect others to share them Quote Link to comment https://forums.phpfreaks.com/topic/91696-best-practice-interceptors/#findComment-469628 Share on other sites More sharing options...
daveuk Posted February 19, 2008 Author Share Posted February 19, 2008 Hi aschk, Ok, thanks for the feedback. So if I create my class and expose the properties using just methods like getAge & setAge, this will be pretty standard? So, I'm assuming that the following would be the norm: $p->setAge(($p->getAge() + 10)); // age = 35 Cheers Dave Quote Link to comment https://forums.phpfreaks.com/topic/91696-best-practice-interceptors/#findComment-470370 Share on other sites More sharing options...
aschk Posted February 19, 2008 Share Posted February 19, 2008 Yup looks good Quote Link to comment https://forums.phpfreaks.com/topic/91696-best-practice-interceptors/#findComment-470415 Share on other sites More sharing options...
able Posted February 19, 2008 Share Posted February 19, 2008 I don't have a problem with __get and __set however the lack of intelisense/code complete and also the fact zend studio now has get/set generation for variables means I'd stick with the full methods. Quote Link to comment https://forums.phpfreaks.com/topic/91696-best-practice-interceptors/#findComment-470560 Share on other sites More sharing options...
Daniel0 Posted February 19, 2008 Share Posted February 19, 2008 In the example Dave posted in the first post the use of those two magic methods is unnecessary but there are certainly uses for those methods in some cases (although it would mostly be for convenience). Quote Link to comment https://forums.phpfreaks.com/topic/91696-best-practice-interceptors/#findComment-471044 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.