tags:

views:

66

answers:

2

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

+6  A: 

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();

unset($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

Chacha102
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
+2  A: 

Create a class and make the method private.

<?php
class Foo
{
    private $bar = 'baz';

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

    private function doSomethingPrivate()
    {
        return 'blah';
    }
}
?>
JeremySpouken