Somewhat new to OOP - class interaction...?
Moderator: General Moderators
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
Add an intermediary class to remove the DBConnect dependency and replace it with something that leaves the MyClass blind to the DBConnect implementation. It's replacing dependencies, but in a way which is more re-useable. MyClass is now not aware how to create a connection, it uses a name reference to the Registry which will return the requested object. Make sense?
Use code:
The SimpleRegistry is just that - simple. There are a few similar methods with the same core idea, but which make managing the registered instances easier.
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);- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Since there are pros and cons it is not a matter of better -- just different. I usually try to make the dependencies clear when using the class by passing dependencies to the constructor. I also like the flexiblity of being about construct everything with one line or conversely being able to instansiate an object like DBConnect earlier and pass it to several objects throughout the code.
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;
}
}(#10850)
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
I'm going to jump on to this extremely interesting bandwagon, with a couple of questions of my own. Sorry to the thread creator!
These are pretty basic things, I think, but it's something that I can't quite get my head around...
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.
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.
These are pretty basic things, I think, but it's something that I can't quite get my head around...
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).
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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(); } }
1) Con - What if you are using the DBConnect class somewhere else outside of the scope of MyClass? You would either open a connection twice, or you would have to tell the app to close one to open another.
EDIT | Or..... I guess you could instantiate MyClass ($myclass = new MyClass();) and then use the $db object of that class throughtout the script ($myclass->db->do_something_dbish()).
Honestly, I don't know if there are pros and cons to the design.
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
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.
It's topics like this distribute a little new knowledge to everyone...
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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);- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
How about:
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
}(#10850)