user2881809
user2881809

Reputation:

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 ?

Upvotes: 1

Views: 10155

Answers (1)

raidenace
raidenace

Reputation: 12834

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

Upvotes: 8

Related Questions