I want to load data to an instance of an object using its constructor and I write
$this->property=$row["colname"]
each time for each property.
the mysql_fetch_object function fetches the data as an object but I am not sure if the instance of an object can be assigned to some object from inside. othwerwise I would use
__construct($object) { $this=$object; } //doesn't give any syntax error
Maybe I should look into iteration of properties and use
foreach($object as $key => $value) $value=$object[$key];
or can I assign like
$this=$object;
within the constructor?
You can’t assign anything to
$this, or you’ll get an error.I would do something like this:
Then you have the option of load the array into the object at construction time, or after construction via
load().Usage example:
Demo: http://codepad.org/uV7bOrfL