note 115245 deleted from language.oop5.overloading by cmb
| From: | cmb@php.net | Date: | Sun, 29 Dec 2019 10:13:47 +0000 |
| Subject: | note 115245 deleted from language.oop5.overloading by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213664@lists.php.net to get a copy of this message | ||
Note Submitter: bimal at sanjaal dot com
----
Use of magic functions may make your private variables behave like public ones.
But it becomes strict; you cannot (in fact should not) assign any properties dynamically during the
runtime.
<?php
class order
{
private $OrderID='';
private $OrderAmount=0.00;
public function __set($name='', $value='')
{
if(property_exists($this, $name))
{
$this->$name = $value;
}
}
public function __get($name='')
{
$value = null;
if(property_exists($this, $name))
{
$value = $this->$name;
}
return $value;
}
}
$order = new order();
$order->OrderID = '201305062202';
$order->OrderAmount = 23.45;
$order->InvalidMember = 'Missed Assignment';
echo '<pre>', print_r($order, true), '</pre>';
?>
Outputs:
order Object
(
[OrderID:order:private] => 201305062202
[OrderAmount:order:private] => 23.45
)