Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a PHP function only visible within a file?

Tags:

php

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

like image 394
Digo Avatar asked Mar 08 '10 18:03

Digo


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

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

like image 120
Tyler Carter Avatar answered Sep 28 '22 10:09

Tyler Carter


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';
    }
}
?>
like image 26
Michael D. Irizarry Avatar answered Sep 28 '22 09:09

Michael D. Irizarry