Home > Mobile >  PHP : is there a way to forbid use of global/system function in certain class?
PHP : is there a way to forbid use of global/system function in certain class?

Time:11-25

I am using Laravel and there is a helper function app() (the app() function itself is built in in the framework as a global function and I have no intention to modify it) which in my application I want to keep it usable only inside certain classes and not others.

What I want to do is something like following

class Bar {
    public function __construct() {
        forbid_use_of_function('app');
    }

    public function bar() {
        app('SomeService');
    }
}

$foo = new Bar();

// The following line will give error or throw exception 
$foo->bar();

Is this even possible to achieve?

Thanks in advance.

Edit: To give more context, my code has some internal class functions that uses app to create various instances based on various accessibility restrictions. The code is going to be managed by a variety of other people and not myself. When someone else comes and manage my code, I want to avoid the case that he just does not use my internal functions but just use app() since this is the most convenient way (and unintentionally get around the accessibility restriction check).

CodePudding user response:

You want to restrict the visibility or accessibility of a function provided by a third-party service so that it is accessible to certain classes and inaccessible to others. There is no such construct provided by PHP.

  • Related