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

Post Reply
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

class interaction?

Post by Skittlewidth »

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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Post by santosj »

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.
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

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 :D
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

MrPotatoes wrote: 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 :D
That's an approach I hadn't thought about. It might just work.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

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:

Code: Select all

class MyClass {
var $myobj = null;
function _construct($myobj) {
    $this->myobj = $myobj;
}
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($myparam {
    $this->myobj = new MyOtherClass($myparam);
}
(#10850)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

is_a() works with decendants too, last I checked.. I know instanceof does.
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

feyd wrote:is_a() works with decendants too, last I checked
Yup, sure does.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

feyd wrote:is_a() works with decendants too, last I checked.. I know instanceof does.
Right you are ... I was thinking of duck typing.
(#10850)
Post Reply