[英]PHP How to call a function from another class and another file?
index.php index.php
include('./class1.php');
include('./class2.php');
$Func = new function();
$Func->testfuncton1();
class1.php class1.php
class controller{
public function test(){
echo 'this is test';
}
}
class2.php class2.php
class function{
public function testfuncton1(){
controller::test();
}
}
But we not get content from function test()
. 但是我们不能从功能test()
获得内容。
Tell me please where error ? 告诉我哪里出错了?
Your issues: 您的问题:
class
named function
. 您不能有一个名为function
的class
。 function
is a keyword
. function
是keyword
。 $Func
, but make call using $Function
您初始化$Func
,但使用$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
这应该打印this is a test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.