0

I want some functions to be not visible when required, but visible in the script they belong to.
For example:

required.php:

<?php
function privateFunction() {
    echo 'from private function\n';
}
echo 'from required.php: ';
privateFunction();

index.php:

<?php
require './required.php';
echo 'from index.php: ';
privateFunction(); // I want this to give an error like "private function called outside the script it has been declared".

I have already tried making the function private, but it only gives a parse error.

Muskovets
  • 449
  • 8
  • 16
  • 1
    Why not using OOP? – Sougata Bose Dec 20 '19 at 06:46
  • use Class and make function private. – Devsi Odedra Dec 20 '19 at 06:47
  • @DevsiOdedra I know, but if I do that, the function won't be accessible from the "required.php" script, and I can't mark a class 'private', can I? – Muskovets Dec 20 '19 at 06:48
  • Or use another approach https://stackoverflow.com/questions/12999658/check-if-a-file-was-included-or-loaded to check if file loaded directly or thru include – DuhVir Dec 20 '19 at 06:49
  • dont define class private, make function public , private .. as per your need – Devsi Odedra Dec 20 '19 at 06:50
  • Okay, but I want the function to be accessible ONLY from the 'required.php', and NOT from 'index.php'. – Muskovets Dec 20 '19 at 06:52
  • 1
    You can't make ordinary functions private. Functions are global by design. You could make something like: `$func = function () { ... }`. Then you can use it as `$func();`. That way, you can at least limit it to the scope it's written in and you can unset it when you're done with it. – M. Eriksson Dec 20 '19 at 06:54
  • 1
    If you explain to us _why_ you need this and what it's for, it's going to be _much_ easier for us to come up with a good solution. Right now, it kind of sounds like a [XY problem](http://xyproblem.info/) – M. Eriksson Dec 20 '19 at 07:02

3 Answers3

0

required.php:

<?php
class A {
    private function privateFunction() {
        echo 'from private function\n';
    }
}
phper
  • 1
  • 2
0

This is a probably a bad idea in general (see comments above), but the following should do what you're asking.

It makes use of debug_backtrace() to grab the file name of the function then the file name of the function's caller, and compares them. If they don't match, it throws an exception:

function assertCalledByCurrentScript(): void
{
  $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  if (isset($backtrace[1]) && $backtrace[0]['file'] !== $backtrace[1]['file']) {
    throw new \BadFunctionCallException("Cannot call {$backtrace[1]['function']} from outside of its defining script.");
  }
}

Then use it like this:

function privateFunction()
{
  assertCalledByCurrentScript();

  // rest of your "private" function here
}
Jeto
  • 14,596
  • 2
  • 32
  • 46
0

Basically only option you have is to hide private stuff inside class:

//Your file
class MyClass
{
  public static function myAction()
  {
    $internalStuff = self::internalStuff();
    return 'Using this function you can get what is inside internalStuff: ' . $internalStuff;
  }

  private static function internalStuff()
  {
     return 'Some internal stuff not accessible from outside';
  }
}

function myAction()
{
    return MyClass::myAction();
}

//Otherfile
//require yourfile.php

//You can only use `myAction` as function
echo myAction();

//Or call
echo MyClass::myAction();

//But you cannot use `internalStuff
echo MyClass::internalStuff();

Volvox
  • 1,639
  • 1
  • 12
  • 10