note 80368 added to language.oop5.overloading
| From: | v dot umang at gmail dot com | Date: | Sat, 12 Jan 2008 05:20:13 +0000 |
| Subject: | note 80368 added to language.oop5.overloading | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-135050@lists.php.net to get a copy of this message | ||
If you want to be able to overload a variable from within a class and this is your code:
<?php
class myClass
{
private $data;
public function __set($var, $val)
{
$this->data[$var] = $val;
}
public function __get($var)
{
$this->data[$var] = $val;
}
}
?>
There is a problem if you want to call these variables from within the class, as you you want to
access data['data'] then you can't say $this->data as it will return the array
$data. Therefore a simple solution is to name the array $_data. So in your __get and __set you will
say $this->_data ... rather than $this->data. I.E:
<?php
class myClass
{
private $_data;
public function __set($var, $val)
{
$this->_data[$var] = $val;
}
public function __get($var)
{
$this->_data[$var] = $val;
}
}
?>
Umang
----
Server IP: 216.92.131.65
Probable Submitter: 59.92.168.254
----
Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php
Edit -- https://master.php.net/note/edit/80368
Del: integrated -- https://master.php.net/note/delete/80368/integrated
Del: useless -- https://master.php.net/note/delete/80368/useless
Del: bad code -- https://master.php.net/note/delete/80368/bad+code
Del: spam -- https://master.php.net/note/delete/80368/spam
Del: non-english -- https://master.php.net/note/delete/80368/non-english
Del: in docs -- https://master.php.net/note/delete/80368/in+docs
Del: other reasons-- https://master.php.net/note/delete/80368
Reject -- https://master.php.net/note/reject/80368
Search -- https://master.php.net/manage/user-notes.php