Trongate Docs
switch to dark modeswitch to dark mode
»
»
Preventing Methods From Being Invoked Via The URL

Preventing Methods From Being Invoked Via The URL

Sometimes you may not want some of your methods to be accessible via the URL.  For those situations, there are three ways to prevent your methods from being invoked (accessed / triggered) via the URL. They are:


Method One: Private Methods

A private method is a method that can only be invoked when it is called directly from within the class where it resides. In PHP, a method can be made private by simply adding the word 'private' before the function declaration. For example:

private function apply_discount($price) {

    if ($price>100) {
     $discount = $price*0.2;
     $price = $price - $discount;
    }

    return $price;
}


Method Two: Protected Methods

A protected method is a method that can be invoked within its parent class and by classes derived from that class. Methods can be made to be protected by simply adding the word 'protected' before the method declaration. For example:

protected function apply_discount($price) {

    if ($price>100) {
        $discount = $price*0.2;
        $price = $price - $discount;
    }

    return $price;
}


Method Three: The Underscore Shortcut

An easier and faster way to make methods impossible to invoke via the URL is simply to make the first character of the method name an underscore. For example:

function _apply_discount($price) {
   
    if ($price>100) {
        $discount = $price*0.2;
        $price = $price - $discount;
    }

    return $price;
}

Just To Let You Know
The underscore shortcut technique produces methods that can be called from other classes and modules, anywhere in your application. If you regard this as being a potential problem then making methods 'private' would be a more advisable approach. Nevertheless, for the purposes of speed and convenience, this documentation will favour the 'Underscore Shortcut' technique.


HELP & SUPPORT

If you have a question or a comment relating to anything you've see here, please goto the Help Bar.

 
×