How can functions of one class be accessed from another class in PHP without using inheritance?
To access functions of one class from another class in PHP without using inheritance, you can create an instance of the class containing the functions within the other class. This allows you to call the functions of the first class through the instance variable.
class Class1 {
public function function1() {
echo "Function 1 called";
}
}
class Class2 {
public function accessFunctionFromClass1() {
$class1 = new Class1();
$class1->function1();
}
}
$class2 = new Class2();
$class2->accessFunctionFromClass1(); // Output: Function 1 called
Keywords
Related Questions
- What potential issues can arise when manipulating numerical data in PHP for use with external services like Google?
- Is it possible to use an API instead of directly scraping content from a webpage in PHP to achieve the desired functionality?
- Is there a way to inform the user of the file they have selected in a "File-Input" field in PHP?