Hey all.
I've been working in PHP5 for a couple of years now and have developed a lightweight MVC framework that I use to speed up site development. Works great, automated formbuilder, autoSQL module, etc.
One bad habit that I have developed, however, is relying on $this as an easy object param. I have yet to figure out how to truly encapsulate data without having to extend from a base class (where all site variables are defined) and using a setter method to populate each object instance with the passed $this object. Everything works, but it's ridiculous that each object has to know about the entire application state. I would like each object to perform it's designated task only requiring a minimum knowledge of the "outside" context.
For example, let's say I have a class that generates a customer's account details:
// pseudo account
Class account extends siteBase {
protected $formObj;
public $custID = 1;
public $name;
public $email;
// etc...
function __construct() {
$this->formObj = new FormBuild($this); // easy $this passing
$this->formObj->getFormData();
$this->formObj->build();
}
}
// pseudo form builder class
Class FormBuild extends siteBase {
protected $data;
function __construct($caller) {
$this->set($caller);
}
function set($arr) {
foreach($arr as $key => $val) {
$this->$key = $val;
}
}
function getFormData() {
$this->data = new FormQuery($this); // more easy $this passing
}
function build() {
foreach($this->data as $key => $val) {
echo $key . " " . $val . "<br>";
}
}
}
Class FormQuery extends siteBase {
function __construct($caller) {
$this->set($caller);
}
function query() {
$sql = "SELECT email, phone FROM account WHERE custID = '$this->custID'";
// query & return return result, etc.
}
}
What do the "pros" do this in situation? I'd love to clean up the interface a bit... thanks!!