class interaction?
Moderator: General Moderators
- Skittlewidth
- Forum Contributor
- Posts: 389
- Joined: Wed Nov 06, 2002 9:18 am
- Location: Kent, UK
class interaction?
I've been struggling to find a way of explaining this. I'm not even sure its a problem!
I have an object from class1 that needs information from an object from class2 to perform an action.
The method in object1 accepts object2 as a parameter then accesses methods/properties of object2 to get the info it needs to carry out a task.
This all relies on the method receiving the correct object from the correct class, or the methods it is expecting won't be there.
I guess I need to test for the parent class of object2 but in PHP 4 i'm not sure how.
Also if another developer were to come along and use the script, how would they know that class1 relied on receiving class2 apart from me writing comments? It is not suitable for one class to be an extension of the other in this case.
An example situation would be a shopping cart, the cart is an object that contains product objects, and can perform functions on the product objects to find out total price etc.
I guess what I'm trying to ask is, is it correct for classes to interact in this way? And if not, what is the alternative? Just trying to make sure I'm doing things properly!
I have an object from class1 that needs information from an object from class2 to perform an action.
The method in object1 accepts object2 as a parameter then accesses methods/properties of object2 to get the info it needs to carry out a task.
This all relies on the method receiving the correct object from the correct class, or the methods it is expecting won't be there.
I guess I need to test for the parent class of object2 but in PHP 4 i'm not sure how.
Also if another developer were to come along and use the script, how would they know that class1 relied on receiving class2 apart from me writing comments? It is not suitable for one class to be an extension of the other in this case.
An example situation would be a shopping cart, the cart is an object that contains product objects, and can perform functions on the product objects to find out total price etc.
I guess what I'm trying to ask is, is it correct for classes to interact in this way? And if not, what is the alternative? Just trying to make sure I'm doing things properly!
I think method_exists would be a better choice, since he is looking for a method and not a class.
EDIT
Okay, both are good.
I would use is_a() and use method_exists to make sure the method exists before you call it. I would set the class up to accept any class that has that method. Unless what you are doing is specific and another class with the same method might not function well with a different class with the method of the same name.
EDIT
Okay, both are good.
I would use is_a() and use method_exists to make sure the method exists before you call it. I would set the class up to accept any class that has that method. Unless what you are doing is specific and another class with the same method might not function well with a different class with the method of the same name.
- MrPotatoes
- Forum Regular
- Posts: 617
- Joined: Wed May 24, 2006 6:42 am
i dunno about using functions like that. they work and i have them in my autoloader but that's it. personally i think the 'best' practice is to actually assign one of the class variables in the class to a new class of the other. so for instance in the ctr or init() you instantate that class and the secondary class has a bunch of setters/getters taht you can use. but, that's just my way 
- Skittlewidth
- Forum Contributor
- Posts: 389
- Joined: Wed Nov 06, 2002 9:18 am
- Location: Kent, UK
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
All good suggestions. The difference between is_a() and method_exists() would be that the former requires the exact class, while the latter allows for polymorphism -- so that depends on your need. Passing the object into the constructor is the common way to inject:
But you can also have the class instansiate a new object in the constructor based on parameters if it must exist.
Code: Select all
class MyClass {
var $myobj = null;
function _construct($myobj) {
$this->myobj = $myobj;
}Code: Select all
class MyClass {
var $myobj = null;
function _construct($myparam {
$this->myobj = new MyOtherClass($myparam);
}(#10850)
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US