RE: PHP object communication

From: Date: Thu, 25 Oct 2001 10:03:46 +0000
Subject: RE: PHP object communication
Groups: php.general 
Request: Send a blank email to php-general+get-72237@lists.php.net to get a copy of this message
If you want access to error functions within the db class it must either extend the error class or have an error object within it. Either ... Class DB extends Error { ... } Class Core extends DB { ... } or Class DB { var $error; ... function DB() // constructor { ... $this->error = new Error(); } ... } Class Core { var $db; var error; function Core() // constructor { ... $this->db = new DB(); $this->error = new Error(); ... } } at the end of the day, both these are ways of getting around the lack of multiple inheritance Tim Ward ---------- From: ad@m.beecher.net [SMTP:ad@m.beecher.net] Sent: 24 October 2001 22:45 To: php-general@lists.php.net Subject: PHP object communication Not coming from a programming background, I'm going to have difficulty explaining this one... :) I would like to handle an entire application through one object, which in turn contains objects. I only recently "got" classes though, and I don't fully understand if what I want to do is possible, or if I'm going about it the wrong way. Consider: <?php // All of these will be in separate files. // The first two are separated simply because // I want to keep my include files small. class Config { // Base config class. // Just contains data. } class Core extends Config { // Base application class. // Just contains methods. } class Error { // Error handler. } class DB { // Database handler. } ?> Tying the first two together is no problem, I can just extend the Config class. However I would like to incorporate the third and fourth as objects inside the main object: <? // In fact these would be instantiated (is that the // right word?) inside the Core class. $Core = new Core; $Core->Error = new Error; $Core->DB = new DB ?> This is fine when working in the main application, or in the $Core object, but where I get stuck is with communication between the $Core->Error and $Core->DB objects. Say for example that within the DB class I encountered an error, and wanted to talk to the Error object - I /could/ do this: <? class DB { var $Error; function DB() { global $Core; $this->Error = $Core->Error; } } ?> But if I extend the application later and add a bunch of new classes, it means I have to do the same for each object I add. I get the impression that I might be able to do this with references, but I can't get a handle on them for the life of me. Can someone explain? Sorry for the lengthy post. Thanks, adam

« previous php.general (#72237) next »