note 72939 deleted from language.oop5.visibility by didou
| From: | didou@php.net | Date: | Mon, 05 Feb 2007 03:17:04 +0000 |
| Subject: | note 72939 deleted from language.oop5.visibility by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-122859@lists.php.net to get a copy of this message | ||
Note Submitter: Eamon Straughn eamon at gizzle dot co dot uk
Reason: spam
----
using visibility is a key to OOP & OOD design. To my amazement it seems most developers
aren't using these key features properly.
say we are creating a person and we only needed to know their height, weight & gender.
BAD CLASS DESIGN
<?php
class A
{
public $height;
public $weight;
public $gender;
public $name;
public function person()
{
return array($height,$weight,$gender,$name)
}
}
?>
this defeats the whole point of OO, would i implement this if i was in ASP.NET, JAVA, infact any OO
lanaguage, the answer would be no. because anyone can change any property of that object, the
structure is weak and isn't scalable. Try using stricter methods that restricts such tampering
& allows scalability.
<?php
Class B
{
protected $property = array();
public function __get($id)
{
return (isset($this->property[$id])) ? $this->property[$id] : null;
}
public function __set($id,$val)
{
$this->property[$id] = $val;
}
}
?>
although i used magic functions here the aim is keep all your class vars with as much restrictions
as posible. This can now be scalabale as well say you didn't just want to get the persons
attributes but wanted to extend that into what car they drive and the features of the car. then it
is much easier that the first example. After seeing many post on this manual it just upsetted me
that visibility is always taken for granted.
Eamon...