简体   繁体   中英

PHP How to call a function from another class and another file?

index.php

include('./class1.php');
include('./class2.php');

$Func = new function();
$Func->testfuncton1();

class1.php

class controller{

  public function test(){
    echo 'this is test';
  }
}

class2.php

class function{

  public function testfuncton1(){
    controller::test();
  }
}

But we not get content from function test() .

Tell me please where error ?

Your issues:

  • You CANNOT have a class named function . function is a keyword .
  • You initialize $Func , but make call using $Function

If you remove these two issues, your code will work correctly:

class ClassController{

  public function test(){
    echo 'this is test';
  }
}

class ClassFunction{

  public function testfuncton1(){
    ClassController::test();
  }
}

$Func = new ClassFunction();
$Func->testfuncton1();

This should print this is a test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM