Posted: Fri May 26, 2006 4:14 am
So, can you show me a better way of achieving what the same thing as i posted above?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
class DBConnect
{
function DBConnect()
{
// connect to database and a whole host of other functions
}
}
class SimpleRegistry
{
var $instances;
function register($name, &$object) {
$this->instances[$name] = $object;
}
function get($name) {
return $this->instances[$name];
}
}
class MyClass
{
var $db;
function MyClass(&$registry)
{
$this->db = $registry->get('dbconn');
}
}Code: Select all
$registry = new SimpleRegistry();
$dbconn = new DBConnect();
$registry->register('dbconn', $dbconn);
$myclass = new MyClass($registry);Code: Select all
class DBConnect
{
function DBConnect($config)
{
// connect to database and a whole host of other functions
}
}
class MyClass
{
var $db;
function MyClass($db)
{
$this->db = $db;
}
}Code: Select all
Class Store {
var $freeSpace;
// some methods
}
Class Article {
var $volume;
// some methods;
}Something that come to mind right away...Pimptastic wrote:Just to continue this, what are the pro's and con's with this method?
Code: Select all
class DBConnect { function DBConnect() { // connect to database and a whole host of other functions } } class MyClass { var $db; function MyClass() { $this->db = new DBConnect(); } }
Not entirely sure I understand you. Is Article being stored in Store? Is there a size restriction? Seems more logical to assume Article would not use Store itself, rather something else would pass Article into Store. If that's the case neither class is dependent on the other...Given that these are two separate classes, how do I affect the Store object from within the Article object? For instance, I might assume that all Articles are in the Store, and so when I create a new Article, I need to reduce the free space in the Store.
That's what I'm talking about. This thread alone has excited me a little bit about OOPMaugrim_The_Reaper wrote:It's topics like this distribute a little new knowledge to everyone.... I live for discussions like these when I don't fully understand the topic.
In this case I think you can pass class vars to one another as vars...GM wrote:Let's say I have 2 classes, a Store class (think of it like a warehouse), and an Article class (something to be stored in the warehouse).
When I move the article into the store, I need to reduce the $freeSpace by $volume.Code: Select all
Class Store { var $freeSpace; // some methods } Class Article { var $volume; // some methods; }
Given that these are two separate classes, how do I affect the Store object from within the Article object? For instance, I might assume that all Articles are in the Store, and so when I create a new Article, I need to reduce the free space in the Store.
I don't understand how I can make the two classes "related" to each other.
Code: Select all
Class Store {
var $freeSpace;
// some methods
funtion Store() {
// contruct
}
function reduce_freespace($article) {
// perform reductions using your article object
}
}
Class Article {
var $volume;
// some methods;
}
$store = new Store();
$article = new Article();
$reduction = $store->reduce_freespace($article->volume);I wouldn't necessarily agree - we try to use classes in much the same way, but invariably we wire them differently depending on our intended design. I would think most of us using OOP tend towards similar practices over time. Of course time, design and "effort" constraints might hide that. I have no problems for example with a big class doing something not likely to change - but if I'm going to be maintaining that class over time because I just know it might change, or because it's part of a larger application, I'll make the extra effort to trim it down into more focused classes since they're usually (im my humble opinion) far easier to maintain and adapt in the long run.A big thing i learned in this topic, is that almost everyone views the "usage" of classes in a different way than the other, I guess its always best to do classes the way you think is logical right for your program, and what makes most sense to you.
Code: Select all
class Warehouse {
var $usedSpace;
var $freeSpace;
// some methods
}
class Space{
var $volume;
// some methods
}
class Article extends Space {
var $type;
// some methods
}