Find out which class called a method in another class
you could use debug_backtrace
, a bit like this :
BTW, take a look at the comments on the manual page : there are some useful functions and advices given ;-)
class Foo
{
public function __construct()
{
$bar = new Bar();
$bar->test();
}
}
class Bar
{
public function test()
{
$trace = debug_backtrace();
if (isset($trace[1])) {
// $trace[0] is ourself
// $trace[1] is our caller
// and so on...
var_dump($trace[1]);
echo "called by {$trace[1]['class']} :: {$trace[1]['function']}";
}
}
}
$foo = new Foo();
The var_dump
would output :
array
'file' => string '/home/squale/developpement/tests/temp/temp.php' (length=46)
'line' => int 29
'function' => string '__construct' (length=11)
'class' => string 'Foo' (length=3)
'object' =>
object(Foo)[1]
'type' => string '->' (length=2)
'args' =>
array
empty
and the echo
:
called by Foo :: __construct
But, as nice as it might look like, I am not sure it should be used as a "normal thing" in your application... Seems odd, actually : with a good design, a method should not need to know what called it, in my opinion.
Java how to call method in another class
You declare a Cypher type static variable c
here. so you can't access the Cypher class method using c
variable without object declaration. But you can call the c
variable from any class because it's a static variable. But your Cypher class don't have any static Method so you can't call these methods without object initialization.
So you must declare a static method or need to create an object for the access method from Cypher class.
But if you declare Cypher type static variable with initialization. then you can call c from any class and also called Chyper class method using the c
variable reference. like:
// Declare this on Runner class
public static Cypher c = new Cypher();
// And then call from any class like
Runner.c.yourMethod();
Happy Coding.
Calling method in another class
You are misusing inheritance, and this fundamental issue is causing your code not to work. In your class above you have
calculation extends tripCostCalculatorUI
you have the calculating class extend the GUI, with hopes that the GUI fields can then be used in your calculation, but this is not what inheritance is for -- it's not present to allow you to connect data, but rather to extend behavior. Yes, your current inheritance set up will allow you to access JTextFields, but (and this is key), these JTextFields are not the same as the ones displayed in the GUI, since they're part of a completely different instance. Your calculation class does not satisfy the "is-a" relationship with the GUI class, and so should not extend it.
Rather, instead you should give the calculation class (which should be renamed Calculation, as all class names should begin with an upper-case letter) methods that take numeric parameters that allow other classes that use this class, including the Gui class, the ability to pass data into the calculation methods, and then get the results that they return.
And so Calculation should use no JTextField variables and instead use the values passed into its calculation method parameters.
So within the GUI's ActionListener, the GUI itself will extract data from its components, convert anything that needs conversion to numeric values, call the appropriate method from the Calculation class to allow a calculation, and then display the result that is returned (after converting the result to text).
Here's a simple example of just what I mean where the GUI and the calculation classes are separate, where you use method parameters:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class SimpleCalcGui extends JPanel {
private static final long serialVersionUID = 1L;
private JTextField field1 = new JTextField(5);
private JTextField field2 = new JTextField(5);
private JTextField resultField = new JTextField(5);
private JButton calcButton = new JButton("Calculate");
public SimpleCalcGui() {
resultField.setFocusable(false);
calcButton.addActionListener(new CalcListener());
add(field1);
add(new JLabel("+"));
add(field2);
add(new JLabel("="));
add(resultField);
add(calcButton);
}
private class CalcListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
// extract the values and convert to numbers
int value1 = Integer.parseInt(field1.getText());
int value2 = Integer.parseInt(field2.getText());
// call MyCalc's method passing in the values
int result = MyCalc.addition(value1, value2);
// display the result
resultField.setText("" + result);
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(calcButton, "Both text fields must have valid numbers",
"Numeric Entry Error", JOptionPane.ERROR_MESSAGE);
field1.setText("");
field2.setText("");
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui() {
SimpleCalcGui mainPanel = new SimpleCalcGui();
JFrame frame = new JFrame("SimpleCalcGui");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}
public class MyCalc {
// overly simple but just to show what I mean
public static int addition(int value1, int value2) {
return value1 + value2;
}
}
Call Class Method From Another Class
update: Just saw the reference to call_user_func_array
in your post. that's different. use getattr
to get the function object and then call it with your arguments
class A(object):
def method1(self, a, b, c):
# foo
method = A.method1
method
is now an actual function object. that you can call directly (functions are first class objects in python just like in PHP > 5.3) . But the considerations from below still apply. That is, the above example will blow up unless you decorate A.method1
with one of the two decorators discussed below, pass it an instance of A
as the first argument or access the method on an instance of A
.
a = A()
method = a.method1
method(1, 2)
You have three options for doing this
- Use an instance of
A
to callmethod1
(using two possible forms) - apply the
classmethod
decorator tomethod1
: you will no longer be able to referenceself
inmethod1
but you will get passed acls
instance in it's place which isA
in this case. - apply the
staticmethod
decorator tomethod1
: you will no longer be able to referenceself
, orcls
instaticmethod1
but you can hardcode references toA
into it, though obviously, these references will be inherited by all subclasses ofA
unless they specifically overridemethod1
and do not callsuper
.
Some examples:
class Test1(object): # always inherit from object in 2.x. it's called new-style classes. look it up
def method1(self, a, b):
return a + b
@staticmethod
def method2(a, b):
return a + b
@classmethod
def method3(cls, a, b):
return cls.method2(a, b)
t = Test1() # same as doing it in another class
Test1.method1(t, 1, 2) #form one of calling a method on an instance
t.method1(1, 2) # form two (the common one) essentially reduces to form one
Test1.method2(1, 2) #the static method can be called with just arguments
t.method2(1, 2) # on an instance or the class
Test1.method3(1, 2) # ditto for the class method. It will have access to the class
t.method3(1, 2) # that it's called on (the subclass if called on a subclass)
# but will not have access to the instance it's called on
# (if it is called on an instance)
Note that in the same way that the name of the self
variable is entirely up to you, so is the name of the cls
variable but those are the customary values.
Now that you know how to do it, I would seriously think about if you want to do it. Often times, methods that are meant to be called unbound (without an instance) are better left as module level functions in python.
How to use methods from another class, called by the client
Creating and using a class by name requires some preconditions:
- Full name of the class must be used, including package name.
- You need to know the parameters of the constructor. Usually the no-arguments constructor is used for a such use case.
- (Optional) You need an interface this class must implement which declares the method "run" (or any other methods you want to use).
An example with a subclass of Runnable
:
String className = "some.classname.comes.from.Client";
Class<Runnable> clazz = (Class<Runnable>) Class.forName(className);
Runnable instance = clazz.getConstructor().newInstance();
instance.run();
If you don't have a common interface, you can invoke the method using reflection:
Class<Object> clazz = (Class<Object>) Class.forName(className);
Object instance = clazz.getConstructor().newInstance();
clazz.getMethod("run").invoke(instance);
or using method with parameters:
Integer p1 = 1;
int p2 = 2;
clazz.getMethod("methodWithParams", Integer.class, Integer.TYPE).invoke(instance, p1, p2);
Related Topics
Create Programmatically a Variable Product and Two New Attributes in Woocommerce
How to Handle Error for Duplicate Entries
What Does This Mean in Documentation: Square Bracket Followed by Comma ( [, )
Using PHP to Populate a <Select></Select> Dropdown
PHP Objects VS Arrays -- Performance Comparison While Iterating
How Exactly Does If($Variable) Work
Mongodb: Fatal Error: Class 'Mongoclient' Not Found
Title Case a String Containing One or More Last Names While Handling Names with Apostrophes
How to Install Pdo Driver in PHP Docker Image
Error While Using Pdo Prepared Statements and Limit in Query
Remove Exif Data from Jpg Using PHP
Using Password_Hash and Password_Verify
PHP Emitting 500 on Errors - Where Is This Documented
How to Redirect Stdout to a File in PHP
How to Search in an Array with Preg_Match
Using PHP Substr() and Strip_Tags() While Retaining Formatting and Without Breaking HTML