note 11540 deleted from language.oop by didou
| From: | didou@php.net | Date: | Sun, 07 Dec 2003 02:38:08 +0000 |
| Subject: | note 11540 deleted from language.oop by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-61543@lists.php.net to get a copy of this message | ||
Note Submitter: idont@remember.it
----
[Editor's note: Just a quick comment.
Inherently OO languages should theoretically access attributes using methods, but even in existing
ones this is not strictly enforced. PHP is *not* and inherently OO language, but one that has some
object/class support.
This note is talking about a "best practice" recommendation, not a necessity that is
dictated by the language design or implementation.]
In many exemples (and in the official doc) I saw a problem: many access directly the attributs of
the objects. Actually we SHOULD always access an attribut through a method.
<?php
class user{
var $_name;
var $_email;
function user($name,$email){
$this->_name=$name;
$this->_email=$email;
}
function getName(){
return($this->_name);
}
function getEmail(){
return($this->_email);
}
function setName($name){
$this->_name=$name;
}
function setEmail($email){
$this->_email=$email;
}
}
$me = new user("Bill","billclinton@whitehouse.org");
echo "my name is :".$me->getName()."<br>";
echo "and my email :".$me->getEmail()."<br>";
//wrong way
$me->_email="billclinton@retired.com";
//Better way
$me->setEmail("billclinton@retired.com");
//wrong way
echo "oops sorry, here is the right one:".$me->_email."<br>";
//Better way
echo "oops sorry, here is the right one:".$me->getEmail()."<br>";
?>
As you noticed I put a "_" at the beginning of the attributs' names, this is a typo
tip to declare them as private. OF course this is not real as PHP doesn't support the notions
of public, private for methods and attributs.
A poor solution waiting for a better one. :)