ok... i have a fairly simple class that retrievs infomation about a node from a database and stores this infomation as properties of the clas itself. now i would like to extend the functionality of this class to enable me to set these properties manually and then call a createnew() method to add these new properties to the db.
ive been toying around with a few ideas, but the one i would really like to impliment is the use of the magic get and set methods. im really not sure how else to put it but i have a book that explains it a little but im also a little lost.
if there is anyone out there who knows what im talking about, could you please show me an example using this class.
<?php
class node
{
private $db;
public $id;
public $parent;
public $lbound;
public $ubound;
function __construct()
{
include('class.db.sqlite.php');
$this->db = new sqlite(DBPATH);
}
public function getnode($id)
{
if (!$this->db->result("SELECT parent,lbound,ubound FROM node WHERE id = $id")) {
throw new exception('node does not exist');
}
$result = $db->fetch();
$this->parent = $id;
$this->parent = $result['parent'];
$this->lbound = $result['lbound'];
$this->ubound = $result['ubound'];
}
}
?>
this isn't my actual class, but something i knocked up at work just to use as an example. hope its not too much trouble, much appreciated.Not your class, because it's up to you which properties you want to create with get and set ... if I recall correctly, they'll only be used if there aren't already properties with the names you want to use.
class ComplexNumber
{
private $r, $i;
public function __get($property)
{
switch($property)
{
case 'real';
return $this->r;
case 'imag';
return $this->i;
case 'mag';
return sqrt($this->r*$this->r+$this->i*$this->i);
case 'arg';
return atan2($this->r, $this->i);
}
}
public function __set($property, $value)
{
switch($property)
{
case 'real';
$this->real = $value; break;
case 'imag';
$this->imag = $value; break;
case 'mag';
$this->real = $value*cos($this->arg);
$this->imag = $value*sin($this->arg);
case 'arg';
$this->real = $this->Mag*cos($value);
$this->imag = $this->Mag*sin($value);
}
}
}
$number->real = 42;
echo $number->mag;
$number->arg = M_PI/2;thanks allot mate... i'll take a closer look as soon as im home and on my machine.
ive been toying around with a few ideas, but the one i would really like to impliment is the use of the magic get and set methods. im really not sure how else to put it but i have a book that explains it a little but im also a little lost.
if there is anyone out there who knows what im talking about, could you please show me an example using this class.
<?php
class node
{
private $db;
public $id;
public $parent;
public $lbound;
public $ubound;
function __construct()
{
include('class.db.sqlite.php');
$this->db = new sqlite(DBPATH);
}
public function getnode($id)
{
if (!$this->db->result("SELECT parent,lbound,ubound FROM node WHERE id = $id")) {
throw new exception('node does not exist');
}
$result = $db->fetch();
$this->parent = $id;
$this->parent = $result['parent'];
$this->lbound = $result['lbound'];
$this->ubound = $result['ubound'];
}
}
?>
this isn't my actual class, but something i knocked up at work just to use as an example. hope its not too much trouble, much appreciated.Not your class, because it's up to you which properties you want to create with get and set ... if I recall correctly, they'll only be used if there aren't already properties with the names you want to use.
class ComplexNumber
{
private $r, $i;
public function __get($property)
{
switch($property)
{
case 'real';
return $this->r;
case 'imag';
return $this->i;
case 'mag';
return sqrt($this->r*$this->r+$this->i*$this->i);
case 'arg';
return atan2($this->r, $this->i);
}
}
public function __set($property, $value)
{
switch($property)
{
case 'real';
$this->real = $value; break;
case 'imag';
$this->imag = $value; break;
case 'mag';
$this->real = $value*cos($this->arg);
$this->imag = $value*sin($this->arg);
case 'arg';
$this->real = $this->Mag*cos($value);
$this->imag = $this->Mag*sin($value);
}
}
}
$number->real = 42;
echo $number->mag;
$number->arg = M_PI/2;thanks allot mate... i'll take a closer look as soon as im home and on my machine.