Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - calling function from another file without including it [closed]

Tags:

function

php

I want to be able to call function from anywhere. So i could just write something like this: file.php:foo();. Is it possible?

like image 696
Anagmate Avatar asked Mar 04 '26 23:03

Anagmate


1 Answers

This may not be a solution for you, but it might. You could wrap your functions into namespacing classes.

You could add a class called file with foo as a static function. So instead of calling foo(), you'd call file::foo(). You would then __autoload those namespaced classes. To do that, you would have to somewhere define your autoload function, something like:

function __autoload($class_name) {
    include $class_name . '.php';
}

And then your file.php would look like:

class file {
   public static function foo() {
      //do stuff
   }
}
like image 122
dave Avatar answered Mar 07 '26 13:03

dave