簡體   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