so I have these 2 classes.
class Data {
protected $custnum, $custname, $inorout, $totalqty, $contractqty, $totalsales, $margin, $contract, $paper;
function __set($property, $value)
{
$this->$property = $value;
}
function __get($property)
{
if (isset($this->$property))
{
return $this->$property;
}
}
and
class Salesman {
protected $num, $name;
// protected $repdata = (array) $Data;
protected $repdata = array();
function __construct() {
/开发者_如何转开发/ $this->repdata = new Data;
}
// set undeclared property
function __set($property, $value)
{
$this->$property = $value;
}
function __get($property)
{
if (isset($this->$property))
{
return $this->$property;
}
}
}
and I am having difficulty trying to populate the $repdata object that's in the Salesman class....
like so
$reptemp = new Salesman;
$datatemp = new Data;
$reptemp->num = $repnum2;
$reptemp->name = $repname2;
$datatemp->custnum = $custnum;
$datatemp->custname = $custname;
$datatemp->inorout = "out";
$datatemp->totalqty = $totalqty;
// $reptemp->repdata[$custnum.$datatemp->inorout] = $datatemp;
$reptemp->repdata[] = $datatemp;
$salesmen[$reptemp->num] = $reptemp;
I know that $datatemp is being populated correctly, however when I try to populate the repdata object within the salesmen object, no error shows, but nothing gets populated.
I did try searching for this, but couldn't quite nail down what I'm doing wrong. I am familiar with java objects, however php and the "magic" classes are making my head spin.
Any help would be greatly appreciated.
thanks so much.
This is why the internal array is no reference. You get a copy of the array with its content and you change the copy, not the original one.
You should make repdata public, because if PHP finds an existing field with that name, it is not called the __get() method. Otherwise you have to return it as reference.
In PHP arrays are not threaded as Objects.
Your implementation of __get and __set allows public access to all your protected/private propertys!
These methods are called if a property does not exist or is not accessable, but __get and __set can access protected propertys!
If you a public property declare a public property!
by the way --get and --set should only be used for error handling. If you know you need a num property declare it properly!
精彩评论