hessodreamy
08-17-2012, 02:52 PM
I've got a financial system where I'd like to make many financial properties into instances of a Charge class, which encapsulate tax information etc, rather than constantly defining vat/ex vat variables & properties.
What I'd like to do is allow such properties to be set as normal numbers, partly for ease of coding, and partly for compatibility with legacy code. What would be the best way to do this?
I could use magic functions, I guess:
class Charge
{
public $net;
public $gross;
public $vat;
public $vatRate = 0.2;
public function __construct($net)
{
$this->net = $net;
$this->vat = round($this->net*$this->vatRate,2);
$this->gross = $this->net + $this->vat;
}
}
class Myclass
{
private $price;
public function __set($name,$val)
{
if($name=="price") $this->setPrice($val);
}
public function setPrice($price)
{
//enforce price into charge class
if(! $price instanceof Charge) $price = new Charge($price);
$this->price = $price;
}
}
$class = new MyClass();
$class->price=3;
but I've heard magic functions are pretty slow, also, to get the magic function to work I'd either need to not declare my variable initially (not keen on that), or declare it as private, in which case the magic function would work when reference from outside the class, but not when referenced from within the class or it's subclasses.
Or I could just declare it as private and make sure the setter is used explicitly at all times.
What would be a good approach to this?
**EDIT**
I just came up with another idea - declare all class properties prepended with an underscore eg private $_price;
You can reference properties in the normal way ($class->price) and, as the property will never exist, will always refer to the magic functions. The magic function then knows to access the property usng the underscore eg
private $_price;
public function __set($name,$val)
{
if($name=="price") $this->_price = new Charge($val);//for example
}
This at least gets around the issue of using a consistent reference when referring from within this class or subclasses. Still got a possible speed issue, though.
What I'd like to do is allow such properties to be set as normal numbers, partly for ease of coding, and partly for compatibility with legacy code. What would be the best way to do this?
I could use magic functions, I guess:
class Charge
{
public $net;
public $gross;
public $vat;
public $vatRate = 0.2;
public function __construct($net)
{
$this->net = $net;
$this->vat = round($this->net*$this->vatRate,2);
$this->gross = $this->net + $this->vat;
}
}
class Myclass
{
private $price;
public function __set($name,$val)
{
if($name=="price") $this->setPrice($val);
}
public function setPrice($price)
{
//enforce price into charge class
if(! $price instanceof Charge) $price = new Charge($price);
$this->price = $price;
}
}
$class = new MyClass();
$class->price=3;
but I've heard magic functions are pretty slow, also, to get the magic function to work I'd either need to not declare my variable initially (not keen on that), or declare it as private, in which case the magic function would work when reference from outside the class, but not when referenced from within the class or it's subclasses.
Or I could just declare it as private and make sure the setter is used explicitly at all times.
What would be a good approach to this?
**EDIT**
I just came up with another idea - declare all class properties prepended with an underscore eg private $_price;
You can reference properties in the normal way ($class->price) and, as the property will never exist, will always refer to the magic functions. The magic function then knows to access the property usng the underscore eg
private $_price;
public function __set($name,$val)
{
if($name=="price") $this->_price = new Charge($val);//for example
}
This at least gets around the issue of using a consistent reference when referring from within this class or subclasses. Still got a possible speed issue, though.