Forums / General Questions / Calling the results of other classes in another class file.

Calling the results of other classes in another class file.

Locked

Started by retched on 6 Aug 2010, 6:09 AM · Views: 684

Results 1 to 3 of 3
This thread is locked. New replies are disabled.
6 Aug 2010, 6:09 AM
#1
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

Calling the results of other classes in another class file.

Hey this goes back to the other question I have about using local sales tax mod with reward points... but since this is a general programming question, figured I'd ask here.

How do I call a $this->output[] value from one class file in the class file of another?

Example:

The $this->output[] holds the result of the calculation of one module file (ot_reward_points_discount.php). I want to call that $this->output[] into ot_local_sales_taxes.php.

But I don't know how to get started with the calls. I tried to call it through the class directly but I get the whitepages. Anyone know how should I get started with this?

6 Aug 2010, 10:39 AM
#2
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Re: Calling the results of other classes in another class file.

As a very general answer $this-> is shorthand for the current class. To get results from a different class into the current class, you would need to find out whether than class already exists as an accessible variable elsewhere (there's no general answer to this, you have to know the specific code) or create one by instantiating the class e.g. "$otherClass = new otherClass".

You can then access the data and methods (functions) of the other class by "$other_class->other_class_variable" and "$other_class->other_class_method()".

However, this is packed tight full of gotchas. Your instance of other_class may not contain the same data as the one that Zen Cart already creates as Zen Cart may take actions beyond simple instantiation to shape it. Also there's a risk that your instantiation may affect global or session variables that Zen Cart needs. These again have no general approaches that can be used to evaluate them, only a detailed knowledge of the code in each class and the way in which Zen Cart uses them.

Sorry that this is a bit vague, but wanted to give some flavour of the area into which you're wandering.

Kuroi Web Design and Development | Twitter

(Questions answered in the forum only - so that any forum member can benefit - not by personal message)

6 Aug 2010, 7:26 PM
#3
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

Re: Calling the results of other classes in another class file.

kuroi:

As a very general answer $this-> is shorthand for the current class. To get results from a different class into the current class, you would need to find out whether than class already exists as an accessible variable elsewhere (there's no general answer to this, you have to know the specific code) or create one by instantiating the class e.g. "$otherClass = new otherClass".

You can then access the data and methods (functions) of the other class by "$other_class->other_class_variable" and "$other_class->other_class_method()".

However, this is packed tight full of gotchas. Your instance of other_class may not contain the same data as the one that Zen Cart already creates as Zen Cart may take actions beyond simple instantiation to shape it. Also there's a risk that your instantiation may affect global or session variables that Zen Cart needs. These again have no general approaches that can be used to evaluate them, only a detailed knowledge of the code in each class and the way in which Zen Cart uses them.

Sorry that this is a bit vague, but wanted to give some flavour of the area into which you're wandering.

Kuroi, thanks for your response.

Something told me that I was on the right path when I had to use the "new ThisClass()" method at some point. So far with me using "if (if_this_class_exists) echo "Hey I see that class!"; So it works for the most part. I guess more poking around will probably give me answers but at least I'm on the right path. Thanks Kuroi.