1

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 ?

1
  • Please refer to my answer below... Commented Jul 26, 2014 at 13:55

1 Answer 1

8

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

Sign up to request clarification or add additional context in comments.

10 Comments

As ClassController::test() is not static it should be called from an instantiated object.
@vascowhite - Not necessarily. Non-static functions can be called using classname::function_name() in php.
You initialize $Func, but make call using $Function - it was error in example
2) name function - only for example
@vascowhite: Have you tried it, or only read documentation? :) Show me one version of PHP 5 where it will not work..Do not trust the documentation all the time :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.