note 72939 added to language.oop5.visibility
| From: | EamonStraughneamon at gizzle dot co dot uk | Date: | Mon, 05 Feb 2007 02:36:31 +0000 |
| Subject: | note 72939 added to language.oop5.visibility | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-122845@lists.php.net to get a copy of this message | ||
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...
----
Server IP: 85.116.0.130
Probable Submitter: 80.229.95.154
----
Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php
Edit -- https://master.php.net/note/edit/72939
Del: integrated -- https://master.php.net/note/delete/72939/integrated
Del: useless -- https://master.php.net/note/delete/72939/useless
Del: bad code -- https://master.php.net/note/delete/72939/bad+code
Del: spam -- https://master.php.net/note/delete/72939/spam
Del: non-english -- https://master.php.net/note/delete/72939/non-english
Del: in docs -- https://master.php.net/note/delete/72939/in+docs
Del: other reasons-- https://master.php.net/note/delete/72939
Reject -- https://master.php.net/note/reject/72939
Search -- https://master.php.net/manage/user-notes.php