Somewhat new to OOP - class interaction...?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

So, can you show me a better way of achieving what the same thing as i posted above?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

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?

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');
    }
}
Use code:

Code: Select all

$registry = new SimpleRegistry();
$dbconn = new DBConnect();
$registry->register('dbconn', $dbconn);
$myclass = new MyClass($registry);
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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

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)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Yep, I just find that something like a Registry becomes useful when classes start requiring multiple others, and those constructor parameters start counting upwards past two or three.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

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).

Code: Select all

Class Store {
   var $freeSpace;

   // some methods

}

Class Article {
   var $volume;

  // some methods;
}
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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

don't worry about me... I'm learning a lot... keep discussing plz :D :D
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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();
    }
}
Something that come to mind right away...
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. :?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

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.
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...

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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Maugrim_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.
That's what I'm talking about. This thread alone has excited me a little bit about OOP 8O .
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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).

Code: Select all

Class Store {
   var $freeSpace;

   // some methods

}

Class Article {
   var $volume;

  // some methods;
}
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.
In this case I think you can pass class vars to one another as vars...

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);
Or something along the lines of that. Not sure if referencing the object would be the right thing to do in this case. I am still kinda learning the reference syntax and uses, although the passing of the db object seemed to make sense to me in an earlier post in this thread.
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

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.
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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

This thread has taught me so much... thanks. I am rethinking OOP now.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

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)
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post by GM »

[quote The Ninja Space Goat]This thread has taught me so much... thanks. I am rethinking OOP now.[/quote]

Me too - thanks everyone. I've got some new ideas to play with now![/quote]
Post Reply