繁体   English   中英

PHP如何从另一个类和另一个文件调用函数?

[英]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();
  }
}

但是我们不能从功能test()获得内容。

告诉我哪里出错了?

您的问题:

  • 您不能有一个名为functionclass functionkeyword
  • 您初始化$Func ,但使用$Function进行调用

如果删除了这两个问题,您的代码将正确运行:

class ClassController{

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

class ClassFunction{

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

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

这应该打印this is a test

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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