Jump to content

Best practice - interceptors


daveuk

Recommended Posts

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;

 

?>

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.