Take the 2-minute tour ×

How to create a PHP function only visible within a file? Not visible to external file. In other word, something equivalent to static function in C

share|improve this question
When you include files on PHP it is as if you are appending them together, in C/C++ you are "importing" the variables/functions/classes. –  Rook Mar 8 '10 at 22:23
add comment

3 Answers

There is no way to actually make a function only visible within a file. But, you can do similar things.

For instance, create a lambda function, assign it to a variable, and unset it when your done:

$func = function(){ return "yay" };

$value = $func();


This is provided that your script is procedural.

You can also play around with namespaces.

Your best bet is to create a class, and make the method private

share|improve this answer
Closures/lambda functions (note these require at least php 5.3) are probably your only option, besides re-thinking your need hide the function from other files. You probably don't actually need to do this. –  meagar Mar 8 '10 at 18:53
add comment

Create a class and make the method private.

class Foo
    private $bar = 'baz';

    public function doSomething()
        return $this->bar = $this->doSomethingPrivate();

    private function doSomethingPrivate()
        return 'blah';
share|improve this answer
add comment

Use namespace, for apply your own visibility.


share|improve this answer
add comment

Your Answer


