I'm still learning OOP so this might not even be possible (although I would be surprised if so), I need some help calling another classes method. Show For example in
now from Thanks!
Will 23.1k13 gold badges93 silver badges104 bronze badges asked Nov 21, 2012 at 23:29
4
answered Nov 21, 2012 at 23:33
codingbizcodingbiz 25.8k8 gold badges55 silver badges93 bronze badges 8 If they are separate classes you can do something like the following:
What I have done in this example is first
create a new instance of the Also note that I don't instantiate the
answered Nov 21, 2012 at 23:36
PeeHaaPeeHaa 69.9k57 gold badges185 silver badges259 bronze badges 3 You would need to have an instance of ClassA within ClassB or have ClassB inherit ClassA
Without inheritance or an instance method, you'd need ClassA to have a static method
--- other file ---
If you're just looking to call the method from an instance of the class:
--- other file ---
Regardless
of the solution you choose, answered Nov 21, 2012 at 23:33
RawkodeRawkode 21.6k5 gold badges38 silver badges45 bronze badges 1 File 1
File 2
answered Nov 21, 2012 at 23:42
1 How can I call a method from another class in php?“how to call function from another class in php” Code Answer. $classA = new ClassA();. $name = $classA->getName();. echo $name; //Prints John.. How do you access a method from another class?To class a method of another class, we need to have the object of that class. Here, we have a class Student that has a method getName() . We access this method from the second class SimpleTesting by using the object of the Student class.
How can I access class method in php?PHP | get_class_methods() Function
The get_class_methods() function is an inbuilt function in PHP which is used to get the class method names. Parameters: This function accepts a single parameter $class_name which holds the class name or an object instance.
How can access static method from another class in php?Static properties are accessed using the Scope Resolution Operator ( :: ) and cannot be accessed through the object operator ( -> ). It's possible to reference the class using a variable. The variable's value cannot be a keyword (e.g. self , parent and static ).
|