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
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
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';
}
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With