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.

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;

?>

    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.

    I've been looking around, and it seems that what you call interceptors are just member variable/method overloading via the magic get(), set(), isset(), unset() and __call() methods.

    This is syntactic sugar, so "best" is a matter of taste.

    Am I best exposing the getAge & setAge classes as public as well as using interceptors?

    I would not do that. I would rather keep the public interface minimal so that client code is forced to be consistent.

    By the way, in the future please indent your code and post it within php bbcode tags. It is also better to use the normal <?php open tag instead of the <? short open tag.

      So, your saying that exposing variables/properties is best done using methods such as getName & setName?

      This is fine with me, I just don't want to get so far down the development path and have to re-develop my classes later 🙂

      Thanks for the note on coding in BB. I will do this in future.

      Cheers

      Dave

      laserlight wrote:

      I've been looking around, and it seems that what you call interceptors are just member variable/method overloading via the magic get(), set(), isset(), unset() and __call() methods.

      This is syntactic sugar, so "best" is a matter of taste.

      I would not do that. I would rather keep the public interface minimal so that client code is forced to be consistent.

      By the way, in the future please indent your code and post it within php bbcode tags. It is also better to use the normal <?php open tag instead of the <? short open tag.

        So, your saying that exposing variables/properties is best done using methods such as getName & setName?

        Nope, I am saying pick a style that suits your taste, and be consistent with it within your script/library/framework.

          Write a Reply...