PHP object communication
| From: | ad at m dot beecher dot net | Date: | Wed, 24 Oct 2001 21:45:05 +0000 |
| Subject: | PHP object communication | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-72171@lists.php.net to get a copy of this message | ||
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