note 80368 deleted from language.oop5.overloading by cmb
| From: | cmb@php.net | Date: | Sun, 18 Aug 2019 08:28:57 +0000 |
| Subject: | note 80368 deleted from language.oop5.overloading by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211904@lists.php.net to get a copy of this message | ||
Note Submitter: v dot umang at gmail dot com
----
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