Natnael Getachew Natnael Getachew - 2 months ago 6
HTML Question

How to load specific functions in php

I am building a website using php and i have used many functions through different pages.

I was thinking of collecting the functions to a single functions.php file and include it on the page i want to use the function but that would also include other functions which are useless on that page, so is there a way to include only the nessesary functions from the functions.php file instead of including them all?

Thanks,

Answer

You may not have the Luxury to pick and choose the functions to load from a File. However, you may try putting all those Functions of yours into a Trait. Then use them within a Trivial Class wherein it would be imported. Here's an Example:

<?php       

        trait PreferredFunctions {

            public function doActionOne($param1){
                // CODE! CODE!! CODE!!!
                return null;
            }

            public function doActionTwo($param2){
                // CODE! CODE!! CODE!!!
                return null;

            }

            public function doActionThree($param3){
                // CODE! CODE!! CODE!!!
                return null;

            }
        }


        class Trivial{
            use PreferredFunctions;

            public function __construct() {
            }
        }

        $trivial    = new Trivial();

        $result1    = $trivial->doActionOne(1);
        $result2    = $trivial->doActionTwo(2);
        $result3    = $trivial->doActionThree(3);